简体   繁体   English

jQuery getjson成功函数不起作用

[英]jquery getjson success function not working

So first, I'm using wampserver as my local web server, my website link from my local environment is http:\\\\localhost\\dev\\mywebsite and I have a local JSON file ( country.json ) in the website root directory ( http:\\\\localhost\\dev\\mywebsite\\country.json ) and then I tried to load that and tried to render the data from that json file using 因此,首先,我将wampserver用作本地Web服务器,本地环境中的网站链接为http:\\\\localhost\\dev\\mywebsite并且网站根目录( http:\\\\localhost\\dev\\mywebsite\\country.json有本地JSON文件( country.json )) http:\\\\localhost\\dev\\mywebsite\\country.json ),然后尝试加载该文件并尝试使用以下命令从该json文件中呈现数据

$.getJSON('http:\\localhost\dev\mywebsite\country.json',function(e){

    console.log(e);

});

but not working like, I can see on my network tab from the developer console (chrome) that the json file was there but this 但无法正常工作,我可以在开发人员控制台(chrome)的“网络”标签上看到json文件存在,但是

console.log(e); 的console.log(E);

were not triggered. 没有被触发。 Any ideas, help please? 有什么想法,请帮忙吗? no errors on the console tho' 控制台上没有错误

Your path is incorrect because \\ is an escape character in JS strings. 您的路径不正确,因为\\是JS字符串中的转义字符。

'http:\\localhost\dev\mywebsite\country.json'
/* ends up looking like this `http:\localhostdevmywebsitecountry.json`*/
/* should be: */
'http://localhost/dev/mywebsite/country.json'

Should use slash not backslash 应该使用斜杠而不是反斜杠

$.getJSON('http://localhost/dev/mywebsite/country.json',function(e){

    console.log(e);// Works OK

});

Or you can use fetch(with Promise like) 或者你可以使用fetch(与Promise一样)

fetch('http://localhost/dev/mywebsite/country.json')
   .then(e => e.json())
   .then(e => console.log(e));

Or you can use fetch(with async/await) 或者您可以使用fetch(与async / await一起使用)

 (async ()=> {
     const res = await fetch('http://localhost/dev/mywebsite/country.json')
     const json = res.json();
     console.log('json', json)
 })();

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

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