我正嘗試使用getJSON
方法獲取我用jQuery編寫的自定義JSON訂閱源。由於一個未知的原因,URL似乎有從最後剝離cache_gen.php?location=PL4
,並用[object%20Object]取代導致發生404錯誤。在jQuery中附加了[object%20Object]的JSON請求
這裏是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
然而在控制檯下聯是:Failed to load resource: the server responded with a status of 404 (Not Found)
。
任何人都可以用這個指向正確的方向嗎?
更新19/01/2013 23: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(它有效地將緩存3第三方服務飼料以節省API的成本)。
http://weatherapp.dev/cache_gen.php?location=PL4不是一個工作的網址。<-----點擊它 – Popnoodles
@popnoodles,它可能是一個'/ etc/hosts'重定向:)。但是,當然'.dev'聽起來很腥 – Alexander
.dev是一個開發領域。它已經在我的本地系統上設置了一個Apache虛擬主機,它在我的''''/ etc/hosts''中有一個條目,以確保正確解析。我可以在我的瀏覽器中訪問這個域,它與加載JS文件的域相同。 –