1

I have a relatively simple POST request in angular...

var data = $.param({
            clientID: '329272'
        });

var config = {
            headers : {
                'Content-Type': 'application/x-www-form-urlencoded;charset=utf-8;'
            }
        }
    $http.post('./server.php', data, config)
        .success(function (data, status, headers, config) {
            console.log(status);
            console.log(data);
        })
        .error(function (data, status, header, config) {
            console.log("nope");
        });

And my php code that receives the data is the following...

$postdata = file_get_contents('php://input');
$request = json_decode($postdata);

header('Content-Type: application/json');
echo json_encode($request);

All I am doing is sending data, then sending it back in the response to check if it worked.

The connection is made, and console prints out a status of 200, but when I console.log(data), I get a value of null. Why am I not able to receive the data?

1
  • done any basic debugging, like var_dump($postdata) to see what got received on the server? Commented Oct 18, 2016 at 16:59

1 Answer 1

1

$.param() does not encode into json so json_decode($postdata) parse on the server will fail.

http://api.jquery.com/jquery.param/

Create a serialized representation of an array, a plain object, or a jQuery object suitable for use in a URL query string or Ajax request.

You can use JSON.stringify to serialize javascript objects into JSON. https://developer.mozilla.org/en-US/docs/Web/JavaScript/Reference/Global_Objects/JSON/stringify

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.