0

I send an AJAX post request in angularjs but it send nothing, since I check on the server side what has arrived which prints nothing using :

print_r($_REQUEST);

However when I use:

print_r(json_decode(file_get_contents("php://input")));

It works since I know the data are sent in JSON chunk and not regular form-format.

Now I have figured out how to send the post data with a header similar to that of jQuery but I still get nothing in server side using regular $_REQUEST or $_POST.

Here is the code block written in Javascript. It is worth mentioning: I have checked all the data of the inputs using console.log() and all of them are defined.

checkUserPostData =   { "username" : registerUsername, "username_check" : 'true'};
        $http.post("server.php",
            checkUserPostData, {"headers" :
            { "Content-Type" : "application/x-www-form-urlencoded; charset=UTF-8" }} )

            .success(function(data, status, header, config){

               console.log(data);
                /*
                if(data=='exists')
                return true;
                else return false;*/

            })

        .error(function(data, status, header, config){
            console.log("error: "+data);
            return data;

        }); // end of $http request

    } // end of CheckUser()

Here is also a log of chrome console on ajax request sent:

Remote Address:::1:8080
Request URL:http://localhost:8080/app/server.php
Request Method:POST
Status Code:200 OK
Request Headersview source
Accept:application/json, text/plain, */*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Connection:keep-alive
Content-Length:44
Content-Type:application/x-www-form-urlencoded; charset=UTF-8
Host:localhost:8080
Origin:http://localhost:8080
Referer:http://localhost:8080/app/
User-Agent:Mozilla/5.0 (Windows NT 6.3; WOW64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/35.0.1916.114 Safari/537.36
Form Dataview sourceview URL encoded
{"username":"asdsa","username_check":"true"}:
Response Headersview source
Connection:Keep-Alive
Content-Length:1231
Content-Type:text/html
Date:Tue, 27 May 2014 12:16:13 GMT
Keep-Alive:timeout=5, max=98
Server:Apache/2.4.4 (Win32) OpenSSL/0.9.8y PHP/5.4.16
X-Powered-By:PHP/5.4.16
Mostafa Talebi
  • 8,825
  • 16
  • 61
  • 105
  • The parameters are not being sent by angularjs to the server in the "form encoded" format. See [this answer](http://stackoverflow.com/a/13989905/238427) (or the other one on that page) for one way to solve your problem. – JoseM May 27 '14 at 13:26
  • possible duplicate of [How can I make angular.js post data as form data instead of a request payload?](http://stackoverflow.com/questions/11442632/how-can-i-make-angular-js-post-data-as-form-data-instead-of-a-request-payload) – JoseM May 27 '14 at 13:28

0 Answers0