本文介绍了离子框架http发帖请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想从离子应用程序向远程php文件发送 POST 请求,以便将base64编码数据保存到数据库中。当我传递 POST 数据时,我可以看到发送了post参数,但是当我从php文件打印响应时,它是空白的。

I want to send a POST request to a remote php file from an ionic app to save base64 encoded data in the database. When i pass POST data, I can see that the post parameter is sent, but when i print the response from the php file, it is blank.

我试过这段代码:

controller.js

controller.js

$http.post("someurl", {data: {"data333": 'peter'}});

当我打印 $ _ POST 或<$来自php的c $ c> $ _ REQUEST 它是一个空白数组,但是从我的控制台我可以看到参数是用json {data:{data333传递的: 'peter'}}

When I print $_POST or $_REQUEST from php, it is a blank array, but from my console I can see that parameters are passed with the json {data: {"data333": 'peter'}}.

我在客户端和服务器上都允许跨域。

I have allowed cross domain in both client and server.

我也试过标准的ajax方式:

I also tried the standard ajax way:

$http.defaults.headers.post["Content-Type"] = 'application/x-www-form-urlencoded; charset=UTF-8';
$http({
        method : 'POST',
        url : 'someurl/',
        headers: {
            'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
        },
data : {"key": "value"}
})

任何人都可以帮我传递数据吗?

Can anyone help me pass the data?

推荐答案

用于将数据传递到服务器的用户对象。希望它对你有所帮助

User object to pass the data to the server. Hope it is helping to you

myobject = { email: user.email,password:user.password };
Object.toparams = function ObjecttoParams(obj)
{
  var p = [];
  for (var key in obj)
  {
    p.push(key + '=' + encodeURIComponent(obj[key]));
  }
  return p.join('&');
};

var req =
{
    method: 'POST',
    url: "API-CALLING-URL",
    data: Object.toparams(myobject),
    headers: {'Content-Type': 'application/x-www-form-urlencoded'}
}

$http(req).
success(function(data, status, headers, config)
{
    //success
}).
error(function(data, status, headers, config)
{
    //error
});

这篇关于离子框架http发帖请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持!

08-19 17:15