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.
I tried this code:
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'}}
.
I have allowed cross domain in both client and server.
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
});
As posted in angularjs-http-post-does-not-send-data, this might be a problem between AngularJS using JSON and PHP expecting form data.
One answer is server-side / PHP:
you can use:
$params = json_decode(file_get_contents('php://input'),true);
To access an array in PHP from an AngularJS POST.
The accepted answer tells you how to convert AngularJS data to a standard post request. It is lengthy. (JSON array to POST string etc)
The body needs to be uri encoded:
$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
I ran into this problem a while ago . Including these headers in PHP api saved my day.
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);
}
View details of my question here
try this it should work, ensure you define $httpParamSerializerJQLike in the controller and youre good to go,
.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'},
})})