-1

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;

?>
nmaier
  • 32,336
  • 5
  • 63
  • 78
user2853597
  • 19
  • 1
  • 9
  • Are you using the SDK or are you building a XUL add-on? Where do you attempt to make the call from? SDK module like main.js? SDK content script? XUL overlay? Javascript code module? etc? – nmaier Nov 05 '13 at 22:55
  • thanks for helping problem is resolved .. – user2853597 Nov 06 '13 at 06:38
  • thanks for your concern ,previous problem is resolved. I have another problem document.body.insertBefore(btn, document.body.firstChild); is not working in mozilla extension in overlay.js. I want to place it on the other youtube element. – user2853597 Nov 06 '13 at 06:42
  • I am working on firefox extesnion and my code is in overlay.js file – user2853597 Nov 06 '13 at 06:44
  • Then ask another question ;) – nmaier Nov 06 '13 at 07:44

2 Answers2

1

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.

Matti Mehtonen
  • 1,685
  • 14
  • 19
  • i have another problem document.body.insertBefore(btn, document.body.firstChild); is not working in mozilla extension in overlay.js. I want to place it on the other youtube element. – user2853597 Nov 06 '13 at 06:41
0

have you tried setting the content type before calling the send?

like this

xmlhttp.setRequestHeader("Content-Type",    "application/x-www-form-urlencoded");
sandy.sk
  • 96
  • 1
  • 6
  • That's only when using `POST` method. OP is using `GET` method. – Matti Mehtonen Nov 05 '13 at 20:39
  • i have another problem document.body.insertBefore(btn, document.body.firstChild); is not working in mozilla extension in overlay.js. I want to place it on the other youtube element. – user2853597 Nov 06 '13 at 06:41