[英]Ionic framework http post request
I want to send a POST
request to a remote php file from an ionic app to save base64 encoded data in the database. 我想从ionic应用程序向远程php文件发送POST
请求,以将base64编码的数据保存在数据库中。 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. 当我传递POST
数据时,我可以看到已发送了post参数,但是当我打印来自php文件的响应时,该参数为空。
I tried this code: 我尝试了这段代码:
controller.js controller.js
$http.post("someurl", {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'}}
. 当我从php打印$_POST
或$_REQUEST
时,它是一个空白数组,但是从我的控制台中,我可以看到参数与json {data: {"data333": 'peter'}}
一起传递。
I have allowed cross domain in both client and server. 我已经允许客户端和服务器中的跨域。
I also tried the standard ajax way: 我还尝试了标准的ajax方法:
$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
});
As posted in angularjs-http-post-does-not-send-data , this might be a problem between AngularJS using JSON and PHP expecting form data. 就像在angularjs-http-post-does-not-send-data中发布的那样 ,这可能是使用JSON的AngularJS和期望表单数据的PHP之间的问题。
One answer is server-side / PHP: 一个答案是服务器端/ PHP:
you can use: 您可以使用:
$params = json_decode(file_get_contents('php://input'),true);
To access an array in PHP from an AngularJS POST. 从AngularJS POST访问PHP中的数组。
The accepted answer tells you how to convert AngularJS data to a standard post request. 接受的答案告诉您如何将AngularJS数据转换为标准的发布请求。 It is lengthy. 很长。 (JSON array to POST string etc) (JSON数组到POST字符串等)
The body needs to be uri encoded: 身体需要使用uri编码:
$http({
method : 'POST',
url : 'someurl/',
transformRequest: $httpParamSerializer,
headers: {
'Content-Type' : 'application/x-www-form-urlencoded; charset=UTF-8'
},
data : {"key": "value"}
})
For more information, see AngularJS $httpParamSerializer Service API Reference 有关更多信息,请参见AngularJS $ httpParamSerializer服务API参考。
I ran into this problem a while ago . 我前一段时间遇到了这个问题。 Including these headers in PHP api saved my day. 在PHP api中包含这些标头可以节省我的时间。
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day
}
// Access-Control headers are received during OPTIONS requests
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0);
}
try this it should work, ensure you define $httpParamSerializerJQLike in the controller and youre good to go, 尝试此操作,确保您在控制器中定义了$ httpParamSerializerJQLike,并且一切顺利,
.controller('myCtrl', function($http, $httpParamSerializerJQLike) {
$http({
method: 'POST',
url: baseUrl,
data: $httpParamSerializerJQLike({
"user":{
"email":"wahxxx@gmail.com",
"password":"123456"
}
}),
headers:
{'Content-Type': 'application/x-www-form-urlencoded'},
})}) })})
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.