Ajax在服务器返回结果时没有显示响应

时间:2012-08-15 06:05:15

标签: php ajax

Ajax向服务器发送GET请求。服务器返回成功结果。

Server response:
[{"result":"success"}] 

但是ajax没有显示任何responseText。 Firebug显示红色的get请求。 enter image description here

Ajax代码部分:

var params = "&id=" +id + "&phone="+phone + "&account_active=" + activeAccount + "&fname="+fname + "&lname="+lname + "&time_zone="+timezone + "&street="+street+"&suite="+suite+"&ring="+ring+"&city="+city+"&state="+state +"&zip="+zip+"&call_time="+callTime +"&call_length="+callLength+"&call_count="+callCount;  

var xmlhttp = getXMLObject();
xmlhttp.open("GET",'agent_manager_response.php?cmd=fsave'+params,true);
xmlhttp.onreadystatechange=function(){
   if(xmlhttp.readyState==4 && xmlhttp.status==200){
    var data = eval(xmlhttp.responseText);
       if(data[0].result == "success"){
        alert('Successfully saved');
       }
      else{
        alert("Error saving data\nError: " + data[0].result);
      }
    }
}
xmlhttp.send(null);

服务器代码:

$result = mysql_query($query,$con);
$data=array();
if(!$result)
     array_push($data,array('result'=>mysql_error()));
else
     array_push($data,array('result'=>'success'));

echo json_encode($data);

Ajax响应DOM选项卡(来自firebug)

DONE 4  
HEADERS_RECEIVED  2
LOADING  3
OPENED   1
UNSENT   0
getchannel  undefined
mozBackgroundRequest  false
multipart    false
onloadend   null
onuploadprogress  null
readyState    4
response     ""
responseText   ""
responseType   ""
responseXML   null
status      0
getstatusText  ""
getupload    undefined
withCredentials   false
abort    abort()
addEventListener  addEventListener()
dispatchEvent   dispatchEvent()
getAllResponseHeaders   getAllResponseHeaders()
getInterface    getInterface()
getResponseHeader   getResponseHeader()
open   open()
overrideMimeType    overrideMimeType()
removeEventListener removeEventListener()
send      send()
sendAsBinary  sendAsBinary()
setRequestHeader   setRequestHeader()

网络标签状态: enter image description here

EDITED

这就是我在网络标签中的全部内容: enter image description here

1 个答案:

答案 0 :(得分:0)

添加内容类型:

xmlhttp.setRequestHeader('Content-Type','application/json');

并改变:

var data = eval(xmlhttp.responseText);

var data = eval( '(' + xmlhttp.responseText + ')' );