Returned JSON is seemingly mixed up when using jQuery Ajax

Posted by Niall Paterson on Stack Overflow See other posts from Stack Overflow or by Niall Paterson
Published on 2012-10-14T15:29:17Z Indexed on 2012/10/14 15:37 UTC
Read the original article Hit count: 279

Filed under:
|
|
|
|

I've a php script that has the following line:

echo json_encode(array('success'=>'true','userid'=>$userid, 'data' => $array));

It returns the following:

{
"success": "true",
"userid": "1",
"data": [
    {
        "id": "1",
        "name": "Trigger",
        "image": "",
        "subtitle": "",
        "description": "",
        "range1": null,
        "range2": null,
        "range3": null
    },
    {
        "id": "2",
        "name": "DWS",
        "image": "",
        "subtitle": "",
        "description": "",
        "range1": null,
        "range2": null,
        "range3": null
    }
]

}

But when I call a jQuery ajax as below:

$.ajax({
    type: 'POST',
    url: 'url',
    crossDomain: true,
    data: {name: name},
    success: function(success, userid, data) {

      if (success = true) {
         document.write(userid);
         document.write(success);
       }
   }
});

The userid is 'success'. The actual success one works, its true.

Is this malformed data being returned? Or is it simply my code?

Thanks in advance, Niall

© Stack Overflow or respective owner

Related posts about php

Related posts about jQuery