1

I have a standard javascript ajax call where I'm setting the data: to json data.

$.ajax({
    type: "POST",
    url: BaseUrl + "User/Login",    
    //url: BaseUrl + "User/Limit/1/2",
    data: '{"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}',
    success: function(data){
        console.log(data);
    },
    error: function(request){
        console.log(request);
    },
});

I was trying to get the data in php $_POST["data"] this doesn't work.
However, data: 'test={"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}' works.

I was wondering is it possibly my framework or anything like that preventing $_POST["data"] from working or is this just not possible at all? Or is there something else I could use to get that data?

EDIT:

So the framework YII and the extension Restfullyii has a method to get the data it is using one line return json_decode(file_get_contents("php://input"), true);

Which is getting all the data without the need for data= or {data: However it seems to be returning an array so Im accessing my properties like $data["userName"] where a true json object should be $data->["userName"]. Correct me if I'm wrong on any of this am I getting array in this case because I'm really sending a json string? versus a json object?

EDIT x2:

So php is making it an assoc array because it is sending true to the json_decode..

MrB
  • 1,544
  • 3
  • 18
  • 30

3 Answers3

3

I think problem with your code is in the line where you set data: '{....}'.
It should be in json format in order to be passed properly (though it also could be in string format but you'll need to parse it on the server side)

The code below should be working right:

$.ajax({
    type: "post",
    url: BaseUrl + "User/Login",
    data: {"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"},
    success: function(data){
        console.log(data);
    },
    error: function(request){
        console.log(request);
    }
});

On the server side try: $_POST['apiKey'] $_POST['appIDGiven'] and so on.

tftd
  • 16,203
  • 11
  • 62
  • 106
  • I'm not certain but I think the data is always sent as string to the server. – MrB Jul 09 '12 at 16:15
  • @MrB Yes in the end I think it's sent as string but the interpretation on the server/client side is different. – tftd Jul 10 '12 at 10:49
1

data option must be an object or serialized(e.g. "name1=value1&name2=value2") string.So you need to pass like this:

data: /*object*/{data:'{"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}'},
                // ^-----this is added for $_POST["data"]

or like:

data: /*serialized string*/'data={"apiKey":"c7089786-7e3a-462c-a620-d85031f0c826","appIDGiven":"200","userName":"matt2","password":"pass"}',
                           // ^-----this is added for $_POST["data"]
Engineer
  • 47,849
  • 12
  • 88
  • 91
0

First, the data sent must be a JSON object and not a string. Remove the quotes.

Also, in your server-side, you'll better decode the input $_POST['data'] with json_decode() (see documentaion)

Nadav S.
  • 2,429
  • 2
  • 24
  • 38