We have developed a web api and are currently attempting to send data via ajax using that api. The web api works perfectly for post requests when tested with the Postman plugin, but we are facing issues when trying to post data through an ajax call using json format.
The specific error we are encountering is XMLHttpRequest cannot load http://localhost:922/api/leaddetails/createlead. Response for preflight has invalid HTTP status code 400
Below is the code snippet for the ajax call:
function SaveLead() {
var Lead = {
"lead_owner": "Pritam",
"company_name": "C",
"title": "T",
"first_name": "Santosh",
"last_name": "M",
"address1": "Rajasthan",
"address2": "d",
"city": "d",
"state": "d",
"country": "d",
"postal_code": "d",
"email": "<a href="/cdn-cgi/l/email-protection" class="__cf_email__" data-cfemail="700611091130171d11191c5e131f1d">[email protected]</a>",
"phone": "d",
"website": "d",
"mobile": "8787878787",
"lead_status": "d",
"lead_source": "d",
"industry": "d",
"annual_revenue": "d",
"skype_id": "d",
"campaign_source": "d",
"description": "d",
"created_by": "A",
"updated_by": "a"
};
$.ajax({
type: "POST",
contentType: "application/json",
url: 'http://localhost:922/api/leaddetails/createlead',
data: JSON.stringify(Lead),
crossOrigin: true,
dataType: "json",
success: function (res) {
alert("The result is : " + res);
},
error: function (xhr) {
alert(xhr.responseText);
}
})
}
Here is a snippet from my web.config file:
<handlers>
<remove name="WebDAV"/>
<remove name="OPTIONSVerbHandler" />
<remove name="TRACEVerbHandler" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_32bit" />
<remove name="ExtensionlessUrlHandler-ISAPI-4.0_64bit" />
<remove name="ExtensionlessUrlHandler-Integrated-4.0" />
(...)
I have successfully tested posting data using the Postman plugin, which worked flawlessly.