2016-02-19 62 views
0

進程函數是爲了定義一個新的ajax請求,然後將其發送到php文件。當輸入的內容輸入到接受capital查詢字符串的文本框中時,控制檯將顯示一條錯誤,指出「SyntaxError:JSON.parse:JSON數據第1行第1列數據的意外結束」這是我的功能JSON解析錯誤意外的數據結束

function process(state, capital) { 


xmlHttp.open("_GET", "statecapitals.php?state=" + state + "&capital=" + capital, true); 

xmlHttp.onreadystatechange = function() { 
    if (xmlHttp.readyState === 4 && xmlHttp.status === 200) {    
     var resultJSON = JSON.parse(xmlHttp.responseText); 
     var result = resultJSON.capital; 

     if (result === 1) { 
      currentScore = score + 2; 
      objScore.innerHTML = currentScore; 
      showState(); 
      console.log(currentScore); 
     }; 


    }; 



}; 

xmlHttp.send(null); 
}; 
+0

你確定你的'xmlHttp.responseText'真的是JSON嗎? – kevin628

+0

是的,其所有有效的 –

+0

你能夠舉一個實際的JSON的例子嗎? json是挑剔的,必須精確地進行格式化,以使「JSON.parse」表現得很好。 – kevin628

回答

0

我認爲你需要在你的代碼首先

console.log(xmlHttp.responseTex); 

使用略低於

if (xmlHttp.readyState === 4 && xmlHttp.status === 200) 

。並檢查它在json中的響應,或者是與任何其他字符串一起附加的響應。

我認爲這可以幫助你。

謝謝

相關問題