How to handle status 302?

When I execute a simple HTTP XML 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 the status code 302 in the FireBug console. According to this article W3 , a status code of 302 means that the resource is temporarily redirected. I'm not quite sure what this means in my case, because when I type my URL in FireFox, I get the answer as it should, looking great.

There are many articles in this article, but I cannot figure it out. For example, this only updates the status code definition. This option suggests using HEAD instead of GET, but this is not an option in my case (if absolutely necessary). Here, he even claimed that the status code 302 automatically leads to the correct redirect to plac where the page moved (as a result, the status code is 200), which is wrong in my case.

I tried looking at xhr.getRespoonseHeader("Content-Type") , but it seems to be null .

I am stuck. What can I do with my problem?

+4
source share
2 answers

Perhaps this is due to some internal redirection on the IIS server. I will also check if the service is installed in the application pool.

In addition, it may be something like protocol switching or domain ambiguity (i.e., secure / insecure http and / or with the / sans w3 prefix).

+1
source

It looks like your server:

  • Redirecting non-www http:// to www http://www.
  • Insecure HTTP redirection http:// to protect https https://

In your web browser, look at http://server/Service1.svc?input1=uno&input2=duo and look at the address bar. Has this changed at all? Check if www or https added.

You can also change your javascript url to version www and version https to see if it fixes this.

Edit : based on what you said about https, I think it redirects http to https. Your ajax code is not suitable to explain why the callback does not work.

You need to use the onreadystatechange event (do not load) and check state 4 , which means that it has 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); 
+2
source

Source: https://habr.com/ru/post/1447531/


All Articles