2011-01-21 5 views
0
$.getJSON("http://www.geoplugin.net/json.gp?ip=117.201.92.17",function(data){alert(data);}); 

このコードはうまく動作しますが、URLの応答は空です。しかし、私は同じURLをコピーしてURLを貼り付けると、それは動作します。なぜどんなアイデア? http://api.jquery.com/jQuery.getJSON/状態にjquery geoplugin使用上の問題

答えて

3

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/>'); 
    } 
); 

は、だから私は働いて、それを表示することやcreated a fiddle here (link)を追加しました。

0

ドキュメント:

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/ .

データが有効なJSON形式で返されますか?他のアプローチしようとしないのはなぜ:

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

この道を、あなたは、JSONデータ型をドロップすると、呼び出しが任意のデータ、JSONを返すかどうか動作することを確認してください可能性があります。

また、ブラウザのセキュリティ上の制約のため、ajaxリクエストには同じ発信元ポリシーが適用されることに注意してください。要求は別のドメイン、サブドメイン、またはプロトコルからデータを正常に取得できません。 this page (link)によると

関連する問題