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);
};
你確定你的'xmlHttp.responseText'真的是JSON嗎? – kevin628
是的,其所有有效的 –
你能夠舉一個實際的JSON的例子嗎? json是挑剔的,必須精確地進行格式化,以使「JSON.parse」表現得很好。 – kevin628