AngularJS HTTP post to PHP and undefined
angularjs .post()
defaults the Content-type header to application/json
. You are overriding this to pass form-encoded data, however you are not changing your data
value to pass an appropriate query string, so PHP is not populating $_POST
as you expect.
My suggestion would be to just use the default angularjs setting of application/json
as header, read the raw input in PHP, and then deserialize the JSON.
That can be achieved in PHP like this:
$postdata = file_get_contents("php://input");
$request = json_decode($postdata);
$email = $request->email;
$pass = $request->password;
Alternately, if you are heavily relying on $_POST
functionality, you can form a query string like email=someemail@email.com&password=somepassword
and send that as data. Make sure that this query string is URL encoded. If manually built (as opposed to using something like jQuery.serialize()
), Javascript's encodeURIComponent()
should do the trick for you.
getting undefined while sending http post method
Not entirely sure about the PHP part, but as you have json_decode
in PHP, its safe to assume that PHP expects a JSON content-type
If so, here is how to post data to a url
var postUrl = 'insert.php'; // please check whether the url is correct
var dto = {
'send_id': $scope.id,
'send_name': $scope.name,
'send_phone': $scope.phone,
'send_status': $scope.status
};
$http({
url: postUrl,
method: 'POST',
data: dto,
headers: {
"Content-Type": "application/json"
}
})
//...
Angularjs $http post request data and response with undefined error
The problem may be in your $http call, my $http calls look like:
$http({
method: 'post',
url:'http://localhost/php/UpdateLastLogin.php',
data: {...},
config: 'Content-Type: application/json;'
}).then(function (response) {
console.log(response);
}, function (response) {
console.log(response);
});
If I'm not wrong, you can use both, headers or config, but seems like you aren't using properly the attribute headers, the documentation says:
- headers – {function([headerName])} – Header getter function.
- config – {Object} – The configuration object that was used to generate the request.
So try to change your 'Content-Type' to 'application/json', because that's what you are sending in you http request body. If it doesn't work, change headers to config to test.
If it doesn't work either, I'd use any tool like, 'postman' to check the API, and make sure that it's working, and try to debug the PHP code (I can't help you with this part)
angular js keep showing me undefined value when i use http post
From the Docs:
$_POST
An associative array of variables passed to the current script via the HTTP POST method when using
application/x-www-form-urlencoded
ormultipart/form-data
as the HTTP Content-Type in the request.— PHP Manual - Reserved variable - $_POST
The AngularJS framework does POST request with application/json
as the HTTP Content-Type.
Instead use:1
$data = json_decode(file_get_contents('php://input'), true);
For more information, see
- PHP Manual - Supported wrappers and protocols - php://
- PHP Manual - Function Reference - file_get_contents
- PHP Manual - Function Reference - json_decode
Angular $http post request undefined in PHP
$post = json_decode(file_get_contents('php://input'), true);
I added this above single line in mailer.php
, my problem solved. Thanks for all replies.
AngularJS HTTP Post to PHP
You need to convert your request to php known
format.
I use jQuery
$.param
method to build it. You can write your own.
app.config(['$httpProvider', function($http) {
$http.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded;charset=utf-8';
$http.defaults.transformRequest = function(data) {
return $.param(data);
};
}]);
urlencoded
is followed simple format:
username=Name&password=My%20Password
JSFiddle
Related Topics
Open_Basedir Restriction in Effect. File(/) Is Not Within the Allowed Path(S):
PHP - Should I Call Exit() After Calling Location: Header
How to Post Json Data With PHP Curl
How to Implement a Good Profanity Filter
Detecting Request Type in PHP (Get, Post, Put or Delete)
How to Call a Function from a String Stored in a Variable
How to Escape Strings in SQL Server Using PHP
Converting String to Date and Datetime
PHP Ini File_Get_Contents External Url
A Simple Program to Crud Node and Node Values of Xml File
Able to See a Variable in Print_R()'S Output, But Not Sure How to Access It in Code
How to Close a Connection Early
How to Connect to Multiple MySQL Databases on a Single Webpage
Invoking a PHP Script from a MySQL Trigger
How to Flush Output After Each 'Echo' Call
Truncate a String to First N Characters of a String and Add Three Dots If Any Characters Are Removed