私は自分の問題に対する答えを求めてstackoverflowを探していましたが、なぜこれが起こっているのかわかりません。
JSONオブジェクトを返すWebサーバーを作成しました。
http://213.125.101.19/api.php?function=test
その後、Ajaxを使用してJSONを呼び出すために次のJavaScriptを使用してHTMLファイルを作成しました
<script language="javascript" type="text/javascript">
<!--
//Browser Support Code
function ajaxCall(){
var ajaxRequest; // The variable that makes Ajax possible!
try{
// Opera 8.0+, Firefox, Safari
ajaxRequest = new XMLHttpRequest();
} catch (e){
// Internet Explorer Browsers
try{
ajaxRequest = new ActiveXObject("Msxml2.XMLHTTP");
} catch (e) {
try{
ajaxRequest = new ActiveXObject("Microsoft.XMLHTTP");
} catch (e){
// Something went wrong
alert("Your browser broke!");
return false;
}
}
}
// Create a function that will receive data sent from the server
ajaxRequest.onreadystatechange = function(){
if(ajaxRequest.readyState == 4){
var response = ajaxRequest.responseText;
obj = JSON.parse(response);
console.log(obj);
if(response.indexOf("Fatal error")>=0){
alert('Error, Try again.');
}else{
document.getElementById("response").value = response;
}
}
}
ajaxRequest.open("GET", "http://213.125.101.19/api.php?function=test", true);
ajaxRequest.send(null);
}
</script>
このコードを実行すると、Firebugが返されます
"SyntaxError:JSON.parse:予期しないデータの終わりobj = JSON.parse(response);"
バリデーターを介してJSONを実行すると、すべてが正常に表示されます。
これを修正する方法はありますか?
よろしく、Luuk