Skip to content Skip to sidebar Skip to footer

How To Add A Custom Http Header To Ajax Request With Js Or Jquery

Does anyone know how to add or create a custom HTTP header using JavaScript or jQuery? i am using below code but it is giving me 405 Method not Allowed. i am using POST Method but

Solution 1:

When you make a cross origin request, the server must provide access control headers (CORS) to give your JavaScript permission to read the response.

A typical request is considered simple, but if you add certain characteristics to it then it stops being simple and becomes a preflighted request.

One of those characteristics is "It sets custom headers in the request", so by adding custom headers, you have made it complex.

Before the browser makes the POST request, it will send the preflight request which is an OPTIONS request.

It must receive a response from the server giving it permission to make the POST request before it will make that POST request.

When the browser makes the OPTIONS request to the server, it is currently returning an error saying "You aren't allowed to make OPTIONS requests to this URL!".

You need to change the server so it:

  • accepts the OPTIONS request
  • responds with the headers that grant permission to make the POST request

Solution 2:

there is no issue in jQuery code this is because you made a cross origin request. try to use in this way https://www.youtube.com/watch?v=EPSjxg4Rzs8

Post a Comment for "How To Add A Custom Http Header To Ajax Request With Js Or Jquery"