2

When I run this, it sends two requests for some reason. When I remove the headers object though it only runs once. Why is it running twice with the headers object?

$.ajax({
    type: "get",
    headers: {
        Time: time
    },
    url: getHost() + "/leaderboard/top?gameId=" + gameId + "&token=" + createToken([gameId, time]),
    dataType: "json",
    complete: function(data){
        showOutput(JSON.parse(data.responseText));
    }
});

When I look in the chrome network inspector, I see that under Method one says GET and the other says OPTIONS. Other than that the request is exactly the same.

On the server I have the following headers set:

this.res.setHeader("Content-Type", "text/json; charset=utf-8");
this.res.setHeader("Access-Control-Allow-Origin", "*");
this.res.setHeader("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept, Time");
Get Off My Lawn
  • 34,175
  • 38
  • 176
  • 338

1 Answers1

1

Under certain conditions(in this case the presents of the time header) for cross origin GET request a pre-flight OPTIONS request is sent first to make sure that the receiving server accepts these kinds of requests.

Do some reading on Cross Origin Resource Sharing and Same Origin Policy.

Musa
  • 96,336
  • 17
  • 118
  • 137