3

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 in Request Method but its showing me OPTION in Request Method. Status Code:405 Method not Allowed, when i am using postman it is working and showing me 200 ok status code. does anyone help me what is the issue is going on. i am using below code in HTML.

$(document).ready(function () {
    $.ajax( {
        url: 'http://api.XXXXXXXXXX.com/SharedServices/SharedData.svc/rest/Authenticate',
        data: {
            "ClientId": "ApiIntegration",
            "UserName": "XXXXX",
            "Password": "XXXXX@123",
            "EndUserIp": "192.168.1.10"
        },
        method: 'POST',
        dataType: 'json',
        headers: {
            "contentType": "application/json",
            "cache-control": "no-cache"},

        success: function (response) {
            console.log(response);
        },
    });
})

what will be correct jQuery code, if there is an issue in my code. here is the screenshot of postman witch i am using.

enter image description here

3
  • 1
    probably a CORS issue in that case Commented Feb 10, 2016 at 9:14
  • This has nothing to do with headers. The OPTION request is a pre-flight to ensure that CORS is supported on the receiving domain. From the 405 response it would appear that it is not, so what you are trying would not work in JS due to the Same Origin Policy. Commented Feb 10, 2016 at 9:15
  • "cache-control": "no-cache" — Why are you trying to set that on a request header?! Commented Feb 10, 2016 at 10:03

2 Answers 2

2

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
Sign up to request clarification or add additional context in comments.

1 Comment

@AjayKumar — The reason that my answer says nothing about changing the jQuery code is because changing the jQuery code is not the solution. The solution is described at the end of the answer, in the section starting: You need to change the server.
0

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

1 Comment

A link to a potential solution is always welcome, but please add context around the link so your fellow users will have some idea what it is and why it’s there. Always quote the most relevant part of an important link, in case the target site is unreachable or goes permanently offline. Take into account that being barely more than a link to an external site is a possible reason as to Why and how are some answers deleted?.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.