2010-04-05 63 views
1

我试图从PHP的Web服务在JavaScript中的http响应,但在Firefox和Chrome中得到空。 PLZ告诉我,我在这里做的错误是我的代码,问题在获取Http响应在铬

function fetch_details() 
{ 
if (window.XMLHttpRequest) 
{ 
    xhttp=new XMLHttpRequest() 
    alert("first"); 
} 
else 
{ 
    xhttp=new ActiveXObject("Microsoft.XMLHTTP") 
    alert("sec"); 
} 
xhttp.open("GET","url.com",false); 
xhttp.send(""); 
xmlDoc=xhttp.responseXML; 
alert(xmlDoc.getElementsByTagName("Inbox")[0].childNodes[0].nodeValue); 
} 

我曾尝试与阿贾克斯还,但我不是在这里得到的HTTP响应是我的代码,请指导我

var xmlhttp = null; 
var url = "url.com"; 
if (window.XMLHttpRequest) 
{ 
    xmlhttp = new XMLHttpRequest(); 
    alert(xmlhttp); //make sure that Browser supports overrideMimeType 
    if (typeof xmlhttp.overrideMimeType != 'undefined') 
    {      
     xmlhttp.overrideMimeType('text/xml'); 
    } 
} 
else if (window.ActiveXObject) 
{ 
    xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
} 
else 
{ 
    alert('Perhaps your browser does not support xmlhttprequests?'); 
} 

xmlhttp.open('GET', url, true); 
xmlhttp.onreadystatechange = function() { 

if (xmlhttp.readyState == 4) 
{ 
    alert(xmlhttp.responseXML); 
} 
}; 

}

//进行实际的请求 xmlhttp.send(null);

我越来越xmlhttp.readyState = 4 xmlhttp.status = 0 xmlhttp.responseText = “”

PLZ告诉我在哪里,我在做错误

+1

请你谈谈你的代码格式。文本字段上方有一个“010101”按钮,用于将代码标记为代码。即使我为你点击它,这个代码仍然太杂乱,所以这次我不打扰。 – deceze 2010-04-05 06:32:46

+0

您是否在提出跨域请求?这是一个常见的情况下得到的状态代码为0. – Anurag 2010-04-05 07:58:20

回答

0

我无法读取任何的那但Chrome有一个JavaScript控制台,可能会告诉你你做错了什么。

+0

thanx为我的回复,但没有得到任何错误 – Bhaskasr 2010-04-05 06:58:41

+0

我不知道为什么得到http.status = 0 – Bhaskasr 2010-04-05 06:59:57

+0

我认为它的跨域问题 – Bhaskasr 2010-04-05 09:54:05

1

您正在做一个跨域请求。

您只能对同一主机执行xmlhttp请求。

0

这是一个cross-domain问题,解决它的服务器响应头应该包含"access-control-allow-origin"

如果您的服务器在PHP编码,标题应该像下面的例子:

<?php 
    header('Content-type: text/html'); 
    header('Access-Control-Allow-Origin: *'); 
    $uri = 'http'. ($_SERVER['HTTPS'] ? 's' : null) .'://'. $_SERVER['HTTP_HOST'] . $_SERVER['REQUEST_URI']; 
    echo('<p>This information has come from <a href="' . $uri . '">' . $uri . '</a></p>'); 
?>