Miles
Miles

Reputation: 113

Calling JQuery Ajax goes to success, but data undefined

Currently, I am using the JQuery Ajax command to call a WCF Service called isValidUser. Using chrome to debug, I can see that the WCF Service is sending what appears to be the correct JSON Response back. The success callback method defined in the JQuery is also getting called. However, the alert message is outputting 'Undefined.'

Below is the snippet of the Client script that I used.

$.ajax({
    type: "GET",
    contentType: "application/json; charset=utf-8",
    url: 'http://localhost:28506/VikingServices.svc/isValidUser/john/doe',
    processData: false,
    dataType: "jsonp",
    jsonpCallback: "callback",
    success: callback,
    error: ServiceFailed
});
function callback(data) {
    alert(data);
}
function ServiceFailed(xhr) {
    alert(xhr.responseText);
    if (xhr.responseText) {
        var err = xhr.responseText;
        if (err)
            error(err);
        else
            error({ Message: "Unknown server error." })
    }
    return;
}

This is the response that I see in the Chrome Developer tools response from the Ajax Request:

{"Name":"Super Admin","Username":"john","id":1}
  1. Is there any reason I can see a correct response in Chrome Dev Tools, but not get any info in the success callback of JQuery? Is it a JQuery Error?
  2. Is there any additional info I can get from JQuery or another way I can format the data variable? It seem that it should simply output the JSON object that I am seeing in the Chrome Dev Tools.
  3. Are there any trivial format issues with the JSON Object I have received in the Chrome Dev Tools?

Upvotes: 1

Views: 640

Answers (1)

Not Superman
Not Superman

Reputation: 116

Your datatype is 'jsonp', therefore your response must be a valid jsonp response. Something like

callback({"Name":"Super Admin","Username":"john","id":1});

Should work. There is also a validator: http://json-p.org/validator.html

Upvotes: 2

Related Questions