jQuery ajax request using jsonp error

后端 未结 4 1777
情话喂你
情话喂你 2020-12-17 02:03

I\'m writing an app and I need to access some json data in the client side from another server. Because of the cross domain issue I plan on using jsonp. jQuery allows me to

相关标签:
4条回答
  • 2020-12-17 02:17

    I encountered this error before, and solved after using jquery-JSONP

    [Example]

    $.getJSON('http://server-url/Handler.ashx/?Callback=DocumentReadStatus',
      {
          userID: vuserID,
          documentID: vdocumentID
      },
      function(result) {
          if (result.readStatus == '1') {
              alert("ACCEPTED");
          }
          else if (result.readStatus == '0') {
              alert("NOT ACCEPTED");
          }
          else {
              alert(result.readStatus);
          }
      });
    
    0 讨论(0)
  • 2020-12-17 02:30

    for parsing external feeds, and catching possible errors, you can use this

     function GetContent(feedUrl)
        {
            var feedApiGetJSON = 'http://ajax.googleapis.com/ajax/services/feed/load?v=1.0&callback=?&q=';
                $.ajax({
                    url: feedApiGetJSON + feedUrl,
                    dataType: 'jsonp',
                    jsonpCallback: 'JsonpCallback'
                });
        }
    
        function JsonpCallback(data) {
            if (data.responseStatus == "200")
                alert(data.responseData.feed.title);
            else
                alert(data.responseDetails);
    
    0 讨论(0)
  • 2020-12-17 02:30

    Replace: datatype with dataType

    0 讨论(0)
  • 2020-12-17 02:32

    When the dataType is jsonp, jquery won't fire the error function automatically. This is described in the documentation under the "error" option:

    Note: This handler is not called for cross-domain script and JSONP requests. see here.

    The workaround is to explicitly specify a timeout as an option. If the server doesn't respond in the specified amount of time then the error function will be called. More about this here.

    0 讨论(0)
提交回复
热议问题