簡體   English   中英

jQuery getjson成功函數不起作用

[英]jquery getjson success function not working

因此,首先,我將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);

});

但無法正常工作,我可以在開發人員控制台(chrome)的“網絡”標簽上看到json文件存在,但是

的console.log(E);

沒有被觸發。 有什么想法,請幫忙嗎? 控制台上沒有錯誤

您的路徑不正確,因為\\是JS字符串中的轉義字符。

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

應該使用斜杠而不是反斜杠

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

    console.log(e);// Works OK

});

或者你可以使用fetch(與Promise一樣)

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

或者您可以使用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