簡體   English   中英

為什么我的jquery ajax的狀態為404?

[英]Why am I getting a status 404 with jquery ajax?

我正在使用Firefox,並嘗試在本地訪問json文件。 javascript文件和test.json在同一文件夾中。 我的網址未定義。

我的文件夾結構是:

root
|- js
| |-script.js
| |-test.json
|- index.html

的script.js

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

test.json

[{
  "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.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM