XMlHttpRequest在Firefox扩展中不起作用 [英] XMlHttpRequest is not working in Firefox extension
问题描述
这是我的Javascript代码。
code> if(window.XMLHttpRequest)
{//代码为IE7 +,Firefox,Chrome,Opera,Safari
xmlhttp = new XMLHttpRequest();
{//代码为IE6,IE5
xmlhttp = new ActiveXObject(Microsoft.XMLHTTP);
}
// alert(str);
xmlhttp.open(GET,server url / folder / file.php?q =+string,true,user,password);
alert();
xmlhttp.onreadystatechange = function()
{
if(xmlhttp.readyState == 4& xmlhttp.status == 200)
{
alert( 响应);
alert(xmlhttp.responseText);
var string = xmlhttp.responseText;
}
}
xmlhttp.send();
这是我的服务器脚本,它会响应。
<?php
header('Access-Control-Allow-Origin:*');
$ q = $ _ GET [q];
echo $ q;
?>
如果添加
xmlhttp.setRequestHeader(X-Requested-With
,XMLHttpRequest);
另外我会使用 xmlhttp.send(null);
因为一些旧的Firefox浏览器需要 null
参数。还有最后一件事情:在第一次声明 onreadystatechange
侦听器之前,我不会调用 xmlhttp.open
。 >
祝你好运,我希望这可以帮助。
The following code is working fine in Google Chrome but not in Firefox. I can't make a request and can't receive a response. I don't know what the problem is?
This is my Javascript code.
if (window.XMLHttpRequest)
{// code for IE7+, Firefox, Chrome, Opera, Safari
xmlhttp=new XMLHttpRequest();
}
else
{// code for IE6, IE5
xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
}
// alert(str);
xmlhttp.open("GET","server url/folder/file.php?q="+"string",true,"user","password");
alert();
xmlhttp.onreadystatechange=function()
{
if (xmlhttp.readyState==4 && xmlhttp.status==200)
{
alert("response");
alert(xmlhttp.responseText);
var string=xmlhttp.responseText;
}
}
xmlhttp.send();
This is my server script which would respond.
<?php
header('Access-Control-Allow-Origin: *');
$q=$_GET["q"];
echo $q;
?>
How about if you add X-Requested-With
header like this:
xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");
Also i would use xmlhttp.send(null);
because some old Firefox browsers requires that null
argument. And one last thing: I wouldn't call xmlhttp.open
before I have first declared onreadystatechange
listener.
Good luck, I hope this helps.
这篇关于XMlHttpRequest在Firefox扩展中不起作用的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!