JQuery Ajax POST to Web API returning 405 Method Not Allowed -


so have jquery ajax request this:

    function createlokiaccount(someurl) {     var d = {"jurisdiction":17}          $.ajax({                 type: "post",                 url:"http://myserver:111/api/v1/customers/createcustomer/",                 data: json.stringify(d),                 contenttype: "application/json; charset=utf-8",                 datatype: "json",                 success: function(data){alert(data);},                 failure: function(errmsg) {                     alert(errmsg);                 }             });     } 

this hitting web api basically:

    [httppost]     public createcustomer.response createcustomer(createcustomer.request request)     {         httpcontext.current.response.appendheader("access-control-allow-origin", "*");     ... 

which when call in chrome gives me:

options http://myserver:111/api/v1/customers/createcustomer/ 405 (method not allowed)  no 'access-control-allow-origin' header present on requested resource.       

when post request fiddler includes "access-control-allow-origin: *" in response header should, suggest api configured correctly, yet (from fiddler) jquery request looks like:

options http://myserver:111/api/v1/customers/createcustomer/ http/1.1 host: myserver:111 connection: keep-alive access-control-request-method: post origin: http://localhost:6500 user-agent: mozilla/5.0 (windows nt 6.1; wow64) applewebkit/537.36 (khtml, gecko) chrome/34.0.1847.116 safari/537.36 access-control-request-headers: accept, content-type accept: / referer: http://localhost:6500/home/replication?interval=1 accept-encoding: gzip,deflate,sdch accept-language: en-us,en;q=0.8,en-gb;q=0.6,it-it;q=0.4,it;q=0.2

so why post request getting turned options request?

first of all, you're adding 1 header, there @ least 3 of them needed:

"access-control-allow-origin", "*"  "access-control-allow-methods", "get, post, put, delete"  "access-control-allow-headers", "content-type, accept" 

secondly, in case if need cors 1 method in controller, way you're adding headers ok. in common it's not right.

asp.net 5 web api 2 offers cors library.

but if you're using web api, can offer solution (not proper, working). add (in global.asax) every request required headers

protected void application_beginrequest(object sender, eventargs e) {     httpcontext.current.response.addheader("access-control-allow-origin", "*");     if (httpcontext.current.request.httpmethod == "options")     {         httpcontext.current.response.addheader("access-control-allow-methods", "get, post, put, delete");         httpcontext.current.response.addheader("access-control-allow-headers", "content-type, accept");         httpcontext.current.response.addheader("access-control-max-age", "1728000");         httpcontext.current.response.end();     }  } 

Popular posts from this blog

c# - ODP.NET Oracle.ManagedDataAccess causes ORA-12537 network session end of file -

matlab - Compression and Decompression of ECG Signal using HUFFMAN ALGORITHM -

utf 8 - split utf-8 string into bytes in python -