简体   繁体   English

为什么我的jquery ajax的状态为404?

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

I'm using Firefox and am trying to access a json file locally. 我正在使用Firefox,并尝试在本地访问json文件。 The javascript file and test.json are in the same folder. javascript文件和test.json在同一文件夹中。 I get an undefined for my url. 我的网址未定义。

My folder structure is: 我的文件夹结构是:

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

script.js 的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 test.json

[{
  "abc": "testing"
 }, {
  "efg": "testing"
}]

Everything looks fine 一切看起来都很好

But url: 'test.json', Should be url: 'js/test.json', 但是url: 'test.json',应为url: 'js/test.json',

Because index.html accesses test.json from parent directory and not JS directory 因为index.html从父目录而不是JS目录访问test.json

Change the URL with respect to index.html . 相对于index.html更改URL。

url: 'js/test.json',

This is not like CSS, the paths should be relative to the page the script gets executed. 这与CSS不同,路径应相对于脚本执行的页面。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM