我的代码适用于xampp,但不适用于000webhost.com

时间:2014-06-23 17:22:13

标签: javascript php json

当我在localhost上测试它时工作正常,但是当我在网站上使用它时,#we; 000webhost.com"控制台写道"坏!" (因为它在代码中)。我不知道为什么它在localhost上运行,为什么不在000webhost.com

有我的ajax:

var xmlhttp = (window.XMLHttpRequest)?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP");

xmlhttp.onreadystatechange=function(){
  if(xmlhttp.readyState==4 && xmlhttp.status==200){
    var respText = xmlhttp.responseText;
    try{
      var json = JSON.parse(respText);
      console.log("GOOD!");
    }
    catch(e){
      console.log("BAD!");
    }
  }
}

xmlhttp.open(method,url + "?" + parameters,true);
xmlhttp.send();

有我的php:

//$content is html code ex: <span>I'm here</span>
$resp = array(
  'r' => true,
  'response'  =>  $content,
);
echo json_encode($resp);

当我删除try时,会出现错误unexpected token <


对不起我的英文,谢谢你的帮助。

2 个答案:

答案 0 :(得分:1)

你有checked your JSON吗?通过错误的声音,听起来你的JSON无效。

答案 1 :(得分:-1)

确保通过OPEN方法中的URL,这是代码中的唯一内容