简体   繁体   中英

Invalid label in trying to retrieve json from a restful webservice

Having this code when running it on firebug, in the console it will show invalid label and also my success callback doesn't fire. after fiddling my way to google about answers i found out that when using jsonp i need to parse the result and also i need to have a callback. Now i'm banging my head against the wall looking for possible answers. Can you help me with this one? THANKS.

    $.ajax( {
        url: 'http://localhost:8732/Service1/data/10',
        type: "GET",
        contentType: "application/json; charset=utf-8",
        dataType: "jsonp",
        processdata:true,
        jsonpCallback: 'mycallback',
        success : function(data) {

           var json = $.parseJSON(data);
           $('#items').html(json);
           alert(json);

        },
        error : function(req, status, ex) {

          alert("Lol" + ex);

        }
        });
      }

Also the alert("Lol"+ex) will prompt LoljQuery16403233689395911671_1329386795307 was not called"

jsonpString

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. As of jQuery 1.5, setting the jsonp option to false prevents jQuery from adding the "?callback" string to the URL or attempting to use "=?" for transformation. In this case, you should also explicitly set the jsonpCallback setting. For example, { jsonp: false, jsonpCallback: "callbackName" }

jsonpCallbackString, Function

Specify the callback function name for a JSONP request. This value will be used instead of the random name automatically generated by jQuery. It is preferable to let jQuery generate a unique name as it'll make it easier to manage the requests and provide callbacks and error handling. You may want to specify the callback when you want to enable better browser caching of GET requests. As of jQuery 1.5, you can also use a function for this setting, in which case the value of jsonpCallback is set to the return value of that function

Code Sample:

<!DOCTYPE html>
<html>
<head>
  <style>img{ height: 100px; float: left; }</style>
  <script src="http://code.jquery.com/jquery-latest.js"></script>
</head>
<body>
  <div id="images">

</div>
<script>
$.getJSON("http://api.flickr.com/services/feeds/photos_public.gne?jsoncallback=?",
  {
    tags: "cat",
    tagmode: "any",
    format: "json"
  },
  function(data) {
    $.each(data.items, function(i,item){
      $("<img/>").attr("src", item.media.m).appendTo("#images");
      if ( i == 3 ) return false;
    });
  });</script>

</body>
</html>

References : http://api.jquery.com/jQuery.getJSON/

Edit : Code 2

$.ajax({
    url: 'http://server:port/path/to/file',
    type: 'GET',
    dataType: 'jsonp',
    cache: false,
    jsonp: '$callback',
    error: function (x, t, r) { alert(x.response.message); },
    success: function (data) {
        $.each(data.d.results, function (i, val) {
            $("#results").append("<div>" + val.name + "</div>");
        });
    }
});

Is the json valid at http://localhost:8732/Service1/data/10 ? stick it in: http://jsonlint.com/ . You could also try catching the error by doing:

var request = $.ajax( {
    ...
});

request.error(function(error) {
    console.log(error);
});

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM