XMlHttpRequest在Firefox扩展中不起作用 [英] XMlHttpRequest is not working in Firefox extension

查看:173
本文介绍了XMlHttpRequest在Firefox扩展中不起作用的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

以下代码在Google Chrome中运行良好,但在Firefox中无法运行。我无法提出要求,也无法收到答复。我不知道是什么问题?

这是我的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屋!

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