简体   繁体   English

XMlHttpRequest在Firefox扩展中不起作用

[英]XMlHttpRequest is not working in Firefox extension

The following code is working fine in Google Chrome but not in Firefox. 以下代码在Google Chrome浏览器中工作正常,但在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. 这是我的Javascript代码。

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: 如果添加这样的X-Requested-With标头又如何:

xmlhttp.setRequestHeader("X-Requested-With", "XMLHttpRequest");

Also i would use xmlhttp.send(null); 我也将使用xmlhttp.send(null); because some old Firefox browsers requires that null argument. 因为某些旧的Firefox浏览器要求使用null参数。 And one last thing: I wouldn't call xmlhttp.open before I have first declared onreadystatechange listener. 最后一件事:在我首先声明onreadystatechange侦听器之前,我不会调用xmlhttp.open

Good luck, I hope this helps. 祝您好运,希望对您有所帮助。

have you tried setting the content type before calling the send? 在致电发送之前,您是否尝试过设置内容类型?

like this 像这样

xmlhttp.setRequestHeader("Content-Type",    "application/x-www-form-urlencoded");

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM