开发者

jquery geoplugin usage problem

开发者 https://www.devze.com 2023-02-06 05:00 出处:网络
$.getJSON(\"http://www.geoplugi开发者_Go百科n.net/json.gp?ip=117.201.92.17\",function(data){alert(data);});
$.getJSON("http://www.geoplugi开发者_Go百科n.net/json.gp?ip=117.201.92.17",function(data){alert(data);});

This code runs fine, but the response of the url, is empty. However if i visit the same URL by copy pasting the url it works. Any ideas why?


According to this page (link):

AJAX and Error: Invalid label

If you are using jQuery for example to do AJAX calls to the JSON webservice, you will probably be seeing the Javascript error Error: Invalid label To eliminate this error, tag jsoncallback=? onto the url when making the jQuery Ajax call to any JSON webservice eg

$.getJSON("http://www.geoplugin.net/json.gp?jsoncallback=?",
function (data) {
  for (var i in data) {
      document.write('data["i"] = ' + i + '<br/>');
  }
);

So I added that and created a fiddle here (link) to show it working.


Docs on http://api.jquery.com/jQuery.getJSON/ state:

Important: As of jQuery 1.4, if the JSON file contains a syntax error, the request will usually fail silently. Avoid frequent hand-editing of JSON data for this reason. JSON is a data-interchange format with syntax rules that are stricter than those of JavaScript's object literal notation. For example, all strings represented in JSON, whether they are properties or values, must be enclosed in double-quotes. For details on the JSON format, see http://json.org/.

Is the data being returned in a valid JSON format? Why not try the other approach of:

$.ajax({
  url: url,
  dataType: 'json',
  data: data,
  success: callback
});

This way you could drop the json dataType and make sure the call is working returning any data, json or not.

Also remember that due to browser security restrictions, ajax requests are subject to the same origin policy; the request can not successfully retrieve data from a different domain, subdomain, or protocol.

0

精彩评论

暂无评论...
验证码 换一张
取 消