Ajax PHP不向我提供任何响应文本 [英] Ajax PHP Not Providing Me With Any Response Text

查看:72
本文介绍了Ajax PHP不向我提供任何响应文本的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试处理脚本以从php站点取回XML文本.但是,它不返回任何数据.这就是我在做什么.

Hi I am trying to work on a script to get back XML text from a php site. However, it is not returning any data. Here is what I am doing.

<head>
<script type="text/javascript">
function getImei()
{
var imei = document.getElementById("imeiInput").value;
if (imei=="")
  {
  return;
  }
if (window.XMLHttpRequest)
  {// code for IE7+, Firefox, Chrome, Opera, Safari
  xmlhttp=new XMLHttpRequest();
  }
else
  {// code for IE6, IE5
  xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
  }


  xmlhttp.onreadystatechange = function()
  {
  if (xmlhttp.readyState==4)
    {
    document.getElementById("txtResult").innerHTML=xmlhttp.responseText;
    }
  } 


xmlhttp.open("GET","http://www.phonesite.com/xml/api/phone.php?IMEI=" + imei,true);
xmlhttp.send();


}
</script>
</head>
<body>
    <input type="textbox" id="imeiInput"/>
    <input type="button" onClick="getImei()" id="btnSubmit" text="Submit"/>
    <p id="txtResult"></p>
</body>
</html>

当我这样做时,xmlhttp.readystate停在1.当我在xmlhttp.send()下方放置"xmlhttp.onreadystatechange = function()"时,它说FireBug中的就绪状态为4,但它会完全跳转无需深入了解该功能即可完成该功能.

When I do it this way xmlhttp.readystate stops at 1. When I put the "xmlhttp.onreadystatechange = function()" below the xmlhttp.send(), it says the ready state is 4 in FireBug but it completely jumps over the function without going inside of it and finishes.

此外,我确实尝试将带有查询字符串的URL直接输入到我的浏览器中,并返回了文本.看着GET请求,当Javascript发送它和我输入它时,它们是相同的.

Also, I did try to enter in the url with the query string directly into my browser and it returned text. Looked at the GET request and they are the same when the Javascript sends it out and when I enter it in.

Date    Fri, 14 Oct 2011 01:34:05 GMT
Server  Apache
Content-Encoding    gzip
Vary    Accept-Encoding
Content-Length  87
Keep-Alive  timeout=15, max=100
Connection  Keep-Alive
Content-Type    text/xml
Request Headersview source
Host    www.phonesite.com
User-Agent  Mozilla/5.0 (Macintosh; Intel Mac OS X 10.7; rv:7.0.1) Gecko/20100101 Firefox/7.0.1
Accept  text/html,application/xhtml+xml,application/xml;q=0.9,*/*;q=0.8
Accept-Language en-us,en;q=0.5
Accept-Encoding gzip, deflate
Accept-Charset  ISO-8859-1,utf-8;q=0.7,*;q=0.7
Connection  keep-alive

我在Firebug中收到xml文本的错误.

I get this error in firebug for the xml text.

XML Parsing Error: no element found Location: moz-nullprincipal:{baefeeae-cd08-9641-a08f-6b3c3396e8a7} Line Number 1, Column 1:

推荐答案

当您请求从服务器返回的响应是XML时,不只是文本,而是使用responseXML读取了该响应

when you request the responce back from the server to be XML not just text you read that response using responseXML

instead of "responseText"; you need for xml "responseXML"

这至少会返回文本并让您前进

this will at least return back the text and get you on your way

 function getImei()
 {

 if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
 else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

 var imei = document.getElementById("imeiInput").value;
 if (imei=="")
   {
   return;
   }

 if(xmlhttp) {
 xmlhttp.open("GET","http://www.phonesite.com/xml/api/phone.php?IMEI=?" + imei, true); 

 xmlhttp.onreadystatechange = function()
 {
 if (xmlhttp.readyState == 4 && xmlhttp.status == 200) {
 document.getElementById("txtResult").innerHTML=xmlhttp.responseText; // if the returned           content type is is "text/xml" innerhtml will strip out the tags

 }
 }
 xmlhttp.send(null);
 }
 }

这篇关于Ajax PHP不向我提供任何响应文本的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆