0

I have used a simple script to get a response from a PHP page, but the response header is empty in firefox and chrome but not in IE. The snippet of my script is:

var request = makeHttpObject();
request.open("GET", url, true); 
request.withCredentials = "true"; 
request.send(); 
if (request.status == 200 && request.readyState == 4)
   {
     alert(request.getAllResponseHeader());
   }

When I check the response header in the HTTP object under the 'Net' tab in debugging, I can see everything.

Can anyone tell what's missing in my script?

xpda
  • 15,585
  • 8
  • 51
  • 82
  • 2
    What does `makeHttpObject()` look like? And why is `true` a string? And I don’t think `getAllResponseHeader` exists. – Ry- Dec 26 '12 at 18:59
  • It's [getAllResponseHeaders()](https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#getAllResponseHeaders()). – The Alpha Dec 26 '12 at 19:09
  • @minitech: I tried all, but with no result...request.readyState is 4 but status is 0 in debugger. makeHttpObject looks like this:- function makeHttpObject() { try {return new XMLHttpRequest();} catch (error) {} try {return new ActiveXObject("Msxml2.XMLHTTP");} catch (error) {} try {return new ActiveXObject("Microsoft.XMLHTTP");} catch (error) {} throw new Error("Could not create HTTP request object."); } – user1930463 Dec 27 '12 at 04:38
  • @user1930463: Try `send(null)` instead of `send()`, maybe... – Ry- Dec 27 '12 at 22:44
  • 1
    It is not a duplicate of http://stackoverflow.com/questions/5614735/jqxhr-getallresponseheaders-wont-return-all-headers, the questions merely have something in common, and this something is not more than tags. – Gangnus Dec 27 '12 at 23:08
  • @minitech: i tried this also...not working, – user1930463 Dec 28 '12 at 05:28

3 Answers3

1

getAllResponseHeaders is specific to IE. To get a specific response header, use getResponseHeader instead.

Asad Saeeduddin
  • 46,193
  • 6
  • 90
  • 139
  • Huh, it doesn’t mention that on MDN: https://developer.mozilla.org/en-US/docs/DOM/XMLHttpRequest#getAllResponseHeaders%28%29 And I just tried it in Firefox, and it exists. – Ry- Dec 27 '12 at 17:07
  • so..what does you suggest for my problem? i am accessing php file of other domain from local file..do i need some handling for this in php file of other domain? – user1930463 Dec 27 '12 at 18:32
  • @minitech That's weird, that was my reason for claiming it is IE specific. Guess I'll dig around the spec to see if I can find a mention of it. `getResponseHeader` on the other hand is standard. – Asad Saeeduddin Dec 27 '12 at 22:17
  • @user1930463 You should use `getResponseHeader` to get each response header individually. – Asad Saeeduddin Dec 27 '12 at 22:18
  • @Asa: I tried with that..but it is not returning anything at all...just a ready state 4 and status 0, status text "error"... – user1930463 Dec 28 '12 at 05:26
  • CAN ANYONE TELL ME WHAT ARE THE APIS OR JS FILES I NEED TO INCLUDE FOR JQUERY TO RUN? I have included these ones:- – user1930463 Dec 28 '12 at 05:34
0

The process you have used is specific to Internet Explorer. Try using jquery for some other Ajax Library for the same, it will work for most of the browsers.

Sample code:

  $.ajax({
      url: 'ajax/test.php?id=sampleId',
      success: function(data) {
        $('.result').html(data);
        alert('Load was performed.');
        alert(data);
      }
    });

and now on the test.php page you can write your server script. This will run on most of the browsers with javascript eanbled.

  • i have tried this also...nothing worked...http://kanti.zxq.net/dinehot/p.php..this is the page i am trying to get response from...please check if you get anything out of it.. – user1930463 Dec 27 '12 at 06:28
-2

With plain HTTP requests, you have to make one for each browser as I remember. You are using only 1 way so it won't work in all browsers because not all browser understand that one. Maybe you can take a look at JQuery. It's simple, fast and it works in all mayor browsers.

Timo
  • 36
  • 1
  • @minitech: so please tell me what should i do? i am trying to call a hosted page from my localhost server... – user1930463 Jan 04 '13 at 13:34
  • @user1930463: If it's cross-domain, well... there's your problem. Do you have access to either the hosted page or the browser? – Ry- Jan 04 '13 at 16:57
  • Them i was reading just a very old tutorial. But if you still want to use JQuery: http://api.jquery.com/jQuery.get/ or http://api.jquery.com/jQuery.post/ – Timo Jan 06 '13 at 11:47