I'm trying to talk with an API using TypeScript and JQuery (from Definitely Typed).
let ajaxsettings: JQueryAjaxSettings = {
url: this.url,
contentType: "application/json",
type: "POST",
data: JSON.stringify(this.apiRequest),
processData: false,
success: ( data, textStatus, jQxhr ) => {
console.log("Response:" + JSON.stringify(data));
},
error: ( jqXhr, textStatus, errorThrown ) => {
console.log("Error Response; " + JSON.stringify(jqXhr));
},
headers: {
"X-UserName": "blahblah",
"X-Password": "blahblah"
},
beforeSend: (request) => {
request.setRequestHeader("X-APIKey", "blahblahblah");
}
};
$.ajax(ajaxsettings);
Making the request and looking at what Fiddler captures is rather odd.
Wrong HTTP verb, and headers are included in Access-Control-Request-Headers not as a standard header.
JQuery 3.2.0, and the latest index.d.ts from Definitely Typed.
I could create a HTTP request in Fiddler:
The request I'm trying to create:
Update
I've tried juggling the dataType to get around preflight checks:
contentType : "text/plain",
method: "POST",
type: "post",
dataType: "json",
Update 2
The API is hosted within IIS Express from Visual Studio 2017 (using .NET Core), and the website is hosted using lite-server. This code works fine, when taking out the custom headers.