getJSON
メソッドを使用してjQueryで記述したカスタムJSONフィードをフェッチしようとしています。不明な理由で、URLcache_gen.php?location=PL4
が最後から削除され、[object%20Object]に置き換えられたため、404エラーが発生したようです。
これが私が使用しているjQueryです:
var fetchData = function() {
if (Modernizr.localstorage) {
var api_location = "http://weatherapp.dev/cache_gen.php";
var user_location = "PL4";
var date = new Date();
console.log(api_location + '?location=' + user_location);
jQuery.getJSON({
type: "GET",
url: api_location + '?location=' + user_location,
dataType: "json",
success: function(jsonData) {
console.log(jsonData);
}
});
} else {
alert('Your browser is not yet supported. Please upgrade to either Google Chrome or Safari.');
}
}
fetchData();
コンソールログから、URL文字列が次のように正しく計算されていることがわかります。http://weatherapp.dev/cache_gen.php?location=PL4
ただし、コンソールの2行目は次のとおりFailed to load resource: the server responded with a status of 404 (Not Found)
です。
誰かがこれで私を正しい方向に向けることができますか?
更新19/01/201323:15
さて、私はちょうどそれがを使用してドキュメントに完全に適合するように変換しました$.ajax
。また、失敗イベントを追加し、それに渡されるすべてのデータをログに記録しました。
var fetchData = function() {
if (Modernizr.localstorage) {
var api_location = "http://weatherapp.dev/cache_gen.php";
var user_location = "PL4";
var date = new Date();
var url = api_location + '?location=' + user_location;
console.log(url);
jQuery.ajax({
type: "GET",
url: api_location + '?location=' + user_location,
dataType: "json",
success: function(jsonData) {
console.log(jsonData);
},
error: function( jqXHR, textStatus, errorThrown ) {
console.log('textStatus: ' + textStatus );
console.log('errorThrown: ' + errorThrown );
console.log('jqXHR' + jqXHR);
}
});
} else {
alert('Your browser is not yet supported. Please upgrade to either Google Chrome or Safari.');
}
}
fetchData();
この後、私のコンソールは私に次の情報を提供します:
http://weatherapp.dev/cache_gen.php?location=PL4
download_api.js:44textStatus: parsererror
download_api.js:45errorThrown: SyntaxError: JSON Parse error: Unable to parse JSON string
download_api.js:46jqXHR[object Object]
JSONフィードのヘッダーが最新であり、フィードが確実に有効なJSONを提供していることを確認しました(サードパーティのサービスフィードを効果的にキャッシュして、APIのコストを節約します)。