Skip to content Skip to sidebar Skip to footer

Unable To Read Data From Ajax (datatype:"jsonp") Call To Web Service

I am using ajax call's to perform POST and GET operations from a WebService hosted on some server. I am using dataType:'jsonp' due to the cross domain issue.I can see the data bein

Solution 1:

jsonpcallback:function(data){}, //What am I supposed to write here so that I can get the JSON data from Padded json

Usually, nothing. You only need to specify the callback if your JSONP service is really atypical. If you do specify it, it needs to be a string.

Likewise you shouldn't set jsonp: false as that will prevent the callback parameter being generated.

You do need a success handler to handle the data though. Having an error handler is also a good idea.

functionsuccessHandler(data) {
    console.log(data)
}

functionerrorHandler(jqXHR, errorType, exception) {
    console.log(errorType, exception);
}

$.ajax({
    url: "url", // Make this the real URL!dataType: "jsonp",
    success: successHandler,
    error: errorHandler
});

Then the JSONP handler needs to actually return JSONP

The Content-Type header returned by the server should be application/javascript

The body should be:

  • The value of the callback key in the query string
  • (
  • Some JSON
  • );

e.g.

jqueryCallback123u54yiyioeuioey8({ "foo": "bar" });

Solution 2:

I'm not a great scripter, but I used AJAX's for my project.. Try this out, it's how it worked for me.

$.ajax({
    type: method,
    url: "url",
    dataType: "jsonp",
    success: function(data){
        console.log(data);
    }
});

Solution 3:

I got this up and running this way:

<scripttype="text/javascript">var jsonpCallback;

  functioncheckDocId() {
    // AJAX request
    $.ajaxSetup({ cache: false });

    jsonpCallback = function(data) {
      $("#result").html(data.html);
    };

    $.ajax({
      type: "GET",
      url: "/secudok/ws/rest/checkdocid/jsonp/" + encodeURIComponent($("#docId").val()),
      dataType: "jsonp",
      jsonpCallback: "jsonpCallback",
      crossDomain: true
    });
  }

</script>

The server returs the data wrapped into the callback function: function({JSON}).

jsonpCallback({"html":"<table>data</table>\n"})

In my example we use HTML, but could be JSON as well

yourCallbackName({"a":"1"; "b":"2"})

Solution 4:

You need to use your console. E.g. firebug if you use firefox, or chrome's development console.

There you should see mistakes in your code.

ajax()

function in jquery takes success: function(data){ // do something with the data } callback.

console.log(//some data) 

is for logging various data at different points in your script.

So

$.ajax({
    type: method,
    url: "url",
    dataType: "jsonp",
    success: function(data){
    console.log(data);
}

});

Is not such a bad idea.

Post a Comment for "Unable To Read Data From Ajax (datatype:"jsonp") Call To Web Service"