Unable to read data from AJAX (dataType:“jsonp”) call to web service

不羁的心 提交于 2020-01-16 19:49:12

问题


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 being sent by the web service on fiddler. I want to access the data which I get from the service and I dont know how do do that.

This is my ajax call:

    $.ajax({
        type: method,
        url: "url",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        jsonp: false,
        jsonpcallback:function(data){},            //What am I supposed to write here so that I can get the JSON data from Padded json
        success: successHandler,
        error: errorHandler
    });

This is the approximation of the json response that I receive from the service:

    {"Ideas":[
               {"Message":null,"IdeaId":1},
               {"Message":null,"IdeaId":1}
             ]
    }  

Any kind of help will be greatly appreciated. I searched through a lot of posts but could not get through.

Thank you in advance.


回答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.

function successHandler(data) {
    console.log(data)
}

function errorHandler(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" });



回答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);
    }
});



回答3:


I got this up and running this way:

<script type="text/javascript">

  var jsonpCallback;

  function checkDocId() {
    // 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"})



回答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.



来源:https://stackoverflow.com/questions/13424884/unable-to-read-data-from-ajax-datatypejsonp-call-to-web-service

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!