4

When I execute a simple XML HTTP Request from my server using the code below

var targetUrl = "http://server/Service1.svc?input1=uno&input2=duo";
document.getElementById("console").innerHTML += "<br>commencing";

var xhr = new XMLHttpRequest();
xhr.onload = function () {
  document.getElementById("console").innerHTML += "<br>callbacking";
  alert(xhr.responseText);
}

xhr.open("GET", targetUrl);
document.getElementById("console").innerHTML += "<br>finishing";

xhr.send();

I get status code 302 in the console of FireBug. According to this W3 article, status code 302 means that the resource is temporarily redirected. I'm not fully certain on what that mean in my case because when I type my URL into the FireFox, I get the response as supposed to, looking great.

There are numerous articles regarding this issue but I can't get it straight. For instance, this one only restates the definition of the status code. This one suggests to use HEAD instead of GET but that's not an option in my case (unless absolutely necessary). Here it's even claimed that the status code 302 automatically leads to a correct redirect to the plac ewhere the page moved to (resulting in status code 200), which in my case isn't true.

I've tried looking at xhr.getRespoonseHeader("Content-Type") but, apparently, it's null.

I'm stuck. What can I do about my problem?

Community
  • 1
  • 1
  • 2
    Do you have a htaccess redirecting non-www to www? – MrCode Nov 21 '12 at 16:04
  • 3
    Have you looked at a tool like Fiddler and see what happens? I am guessing your server is correcting a url, but without seeing code, live demo, it is hard to say. – epascarello Nov 21 '12 at 16:07
  • @MrCode Not sure. I haven't set that explicitly and I don't know how to check if it's enabled (installed?) by default. Are we talking server side (which I'll have a slight problem changing) or client side (where I have full freedom of action)? –  Nov 21 '12 at 16:15
  • @epascarello Not, not Fiddler, only FireBug. The code is posted in the update to my question. I didn't do that before because I don't want to be that guy who posts **everything** and say "*please help*". I want to learn from this, you see. I just realize that I need a slight push. Maybe not so slight... :) –  Nov 21 '12 at 16:17
  • 2
    @CRMconfusee You can tell by the presence of a .htaccess file on the server itself - it will be a plain text file that contains 'rules' for redirection and other little goodies, you (*generally*) wont be able to tell from the client-side. I've given you a +1 on your question as it's an interesting question, but most of all - I'm really impressed by your attitude in that last comment. It's refreshing to see someone not wanting to be... *that guy*. ;) – Fergus In London Nov 21 '12 at 16:44
  • @FergusMorrow I have very limited access to the server, so I won't be able to check for the existence of that file (unless it's viewable by e.g. *http://server/beep.htaccess* or some facility available remotely). Assuming that (a) such a file exists and (b) such file doesn't exist - how can I analyze, debug and resolve my issue? Any suggestions would be great at this point. I'd put a bounty on the question but I lack reputation, still... –  Nov 21 '12 at 16:55

2 Answers2

2

It looks like your server is either:

  • Redirecting non-www http:// to www http://www.
  • Redirecting unsecured HTTP http:// to secure HTTPS https://

In your web browser, view http://server/Service1.svc?input1=uno&input2=duo and observe the address bar. Does it change in any way at all? Check to see if www is added, or https.

You could also change your javascript URL to the www version, and https version, to see if either fixes it.

Edit: based on what you said about https, I think it is redirecting http to https. Your ajax code is not right so that will explain why the callback isn't working.

You need to use the onreadystatechange event (not onload) and check for state 4 which means it completed:

xhr.onreadystatechange = function(){
    if(xhr.readyState == 4){
            document.getElementById("console").innerHTML += "<br>callbacking";
            alert(xhr.responseText);
    }
}
xhr.open("GET", targetUrl, true);
xhr.send(null); 
MrCode
  • 63,975
  • 10
  • 90
  • 112
  • I'm making hte call from URL line using no-www and no-s-in-http version. Are you saying that the server maybe redirects such a call to an other resource that is secure/www'ed? –  Nov 21 '12 at 22:54
  • I've tested the changes. Switching to *www* prefix gave no difference. Switching to *https* gave the only difference that the response header doesn't show. The callback isn't executed in any case... Is it manageable from JS or do I need to handle it by asking the server administrator to change some configuration? –  Nov 22 '12 at 06:21
  • @CRMconfusee see my edit. Your server must be redirecting to https. – MrCode Nov 22 '12 at 08:01
1

It might have to do with some internal redirect on the IIS server. I'd also check if the service is installed in an application pool.

Also, it might be something like a protocol switch or domain ambiguity (i.e. secured/unsecured http and/ or with/sans w3 prefix).

Konrad Viltersten
  • 36,151
  • 76
  • 250
  • 438