I'm using Firefox and am trying to access a json file locally. The javascript file and test.json are in the same folder. I get an undefined for my url.
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"
}]
Everything looks fine
But url: 'test.json',
Should be url: 'js/test.json',
Because index.html accesses test.json from parent directory and not JS directory
Change the URL with respect to index.html
.
url: 'js/test.json',
This is not like CSS, the paths should be relative to the page the script gets executed.
The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.