[英]Why am I getting a status 404 with jquery ajax?
我正在使用Firefox,並嘗試在本地訪問json文件。 javascript文件和test.json在同一文件夾中。 我的網址未定義。
root
|- js
| |-script.js
| |-test.json
|- index.html
$.ajax({
type: 'GET',
url: 'test.json',
dataType: 'jsonp',
crossDomain: true,
error: function(data) {
console.log(data.url);
console.log('error', data);
},
success: function(data) {
console.log('success', data);
},
complete: function() {
console.log('done');
}
});
[{
"abc": "testing"
}, {
"efg": "testing"
}]
一切看起來都很好
但是url: 'test.json',
應為url: 'js/test.json',
因為index.html從父目錄而不是JS目錄訪問test.json
相對於index.html
更改URL。
url: 'js/test.json',
這與CSS不同,路徑應相對於腳本執行的頁面。
聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.