0

First of all I must say that I'm new to AJAX. So the question would sound dumb for most of them But the I would really appreciate any kind of help in this I have been trying to add make an AJAX get request and pass some parameter in the header which fails for some reason. I know its very basic an issue but I have been searching for a fix for the past 4 hours and couldn't find the right solution

function resetSession() {

            $.ajax({

               dataType: "text",
              beforeSend: function (request)
             {
                  request.setRequestHeader("sessionVariable", sessionVariable);
             },
            url: "/reset.php",
            type: "GET",

           success: function(data) {
                 alert("yes");
               },
            error: function(data){
                 alert("no");


              } 
            });



          }

I dont see any issues with this code and I did look for a different one that fixes it for me and couldn't find it Every time the function is called the request URL is right but request method is OPTIONS

Looking forward to an easy fix thanks in advance

1 Answers1

1

I had the same problem. I solved it by not using jQuery. I think they don't have it covered yet.

Your browser is sending an OPTIONS request to find out it the server accepts your request (retarded, ain't it?). This is a part of CORS (read the whole article if you're interested). The server may decline request with altered headers. Referrer header can never be altered.

Try to send normal ajax:

var req = new XMLHttpRequest();
req.open("GET", "/reset.php");
req.onload = function() {
      console.log("SUCESS:", this.responseText);
}
req.onerror = function() {
      console.log("FAILURE:", this.status);
}
req.setRequestHeader("sessionVariable", sessionVariable);
req.send(null);

I'm not sure what the jQuery dataType property is supposed to do. But since you're not sending anything, it probably doesn't matter.

Community
  • 1
  • 1
Tomáš Zato
  • 50,171
  • 52
  • 268
  • 778
  • I will besending "request.setRequestHeader("sessionVariable", sessionVariable);" How do I get to integrate that as well? – Gotham's Reckoning Dec 03 '14 at 00:51
  • 1
    Added, sorry. [Everything about XHR requests](http://www.w3.org/TR/XMLHttpRequest/) is here. The browsers may not have everything implemented that way. – Tomáš Zato Dec 03 '14 at 00:55
  • I see that your solution worked, but wouldn't it be more correct to alter the server code to have the correct response to the OPTIONS request, i.e. `Access-Control-Allow-Headers: x-requested-with`? – bvx89 Dec 03 '14 at 01:04
  • @bvx89 My solution isn't by-passing CORS, it's a legitimate request - not a hack. But thanks for your comment. That explains a lot. But isn't it funny that server must explicitly allow this header - so common among the internet? Do you really think it's correct that there's one more communication step just because of this? – Tomáš Zato Dec 03 '14 at 01:07
  • @TomášZato for some random reason the solution isn't working now I was super excited when I got it after 4 hours of searching Is there any possible reason that it could go to the previous scenario of request method : OPTIONS – Gotham's Reckoning Dec 03 '14 at 01:28
  • 1
    I can't tell. If you have it online I can take a look and add whatever I find out to my answer. Did you read the article about CORS? It's really important that you know what you're doing. – Tomáš Zato Dec 03 '14 at 01:38
  • @TomášZato I will read about that its working now The issue turns out to be about req.setRequestHeader("sessionVariable", sessionVariable); after a refresh of the page it fails but with the session variable its working. very strange :( – Gotham's Reckoning Dec 03 '14 at 01:47
  • That will be something on the PHP's end I guess. We're now talking about something that isn't relevant to the topic here. If you need more help, start [chat](http://chat.stackoverflow.com/) and @mention me in it, I'll eventually reply. – Tomáš Zato Dec 03 '14 at 02:05
  • @TomášZato there was something I had missed when reporting when it go fixed I was just way too excited that I neglected that the request method was still OPTIONS when I set the req.setRequestHeader ELse it is correct as GET. Is there any other way to set request header?? I guess that would fix my issue – Gotham's Reckoning Dec 03 '14 at 17:01
  • @Gotham'sReckoning The `OPTIONS` will always be called if you call `setRequestheader`. It's written in the document I urged you to read. But if nothing is wrong, the GET request will follow. If something **is** wrong, how can I tell you what it is? You didn't tell me anything about your server. – Tomáš Zato Dec 04 '14 at 10:06