1
$.ajax({
    type:           "GET",
    dataType:       "jsonp",
    jsonpCallback:  "jsoncallback",
    //async: false ,
    data: 
    {
        // some data here

    },
    url: "http://mydomain.com/checkRequest.php",
    success: function(data){

    alert("123");

            alert(data.data.NameA);

    },
    error: function(jqXHR, textStatus){
    alert("Request failed: " + textStatus);
    }
});// end of ajax

I can get the following string from the URL below,

([{
        "data": {
            "Rate": "",
            "RoleA": "Student",
            "NameA": "studentD",
            "RoleB": "Tutor",
            "NameB": "tutorB",
            "Give": "0",
            "Get": "1",
            "Accept": "0",
            "RateCounter": ""
        }
    }
]);

http://mydomain.com/checkRequest.php?callback=jsoncallback&nameB=tutorB&roleB=Tutor&get=1&roleA=Student&nameA=studentD&give=0&_=1364082750444

however, it alerts parsererror. May I ask where's going wrong?

If I delete jsonpCallback: "jsoncallback", and change URL to url: "http://mydomain.com/checkRequest.php?jsoncallback=?", there is no parsererror, but it does not able to alert(data.data.NameA);

1
  • 1
    You should really be using the console when working with objects, and not alerts. That way you could see what's really going on, and you could even see what the parseerror was. Commented Mar 24, 2013 at 0:06

1 Answer 1

2

You probably want the jsonp option rather than jsonpCallback:

$.ajax({
    type:           "GET",
    dataType:       "jsonp",
    jsonp:          "jsoncallback",
    // ...
});

jsonp sets the name of the parameter and would match with your other example:

url: "http://mydomain.com/checkRequest.php?jsoncallback=?",

With jsonpCallback, you're instead setting the value, resulting in a URL that contains callback=jsoncallback. The server doesn't appear to expect this and ends up responding without a function name:

([{...}]);

vs. something like:

jQuery191_etc([{...}]);

From the docs:

jsonp

Override the callback function name in a jsonp request. This value will be used instead of 'callback' in the 'callback=?' part of the query string in the url. So {jsonp:'onJSONPLoad'} would result in 'onJSONPLoad=?' passed to the server. [...]

jsonpCallback

Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. [...]

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.