ajax - REST Service, access not allowed on POST -
i created rest service , i'm connecting using jquery ajax , i'm passing , receiving data json. when i'm using works fine when i'm doing post it's not working, it's not entering debug(service).
it's giving me error: xmlhttprequest cannot load http://localhost:23262/getalldrawsshort. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:40464' therefore not allowed access.
and i'm using access-control-allow-origin: * should allow domains
i found similar issues , tried them didn't work me.
code in global.asax (service):
private void enablecrossdmainajaxcall() { httpcontext.current.response.addheader("access-control-allow-origin", "*"); if (httpcontext.current.request.httpmethod == "options") { httpcontext.current.response.addheader("access-control-allow-methods", "post, get, options"); httpcontext.current.response.addheader("access-control-allow-headers", "content-type, accept"); httpcontext.current.response.addheader("access-control-max-age", "1728000"); httpcontext.current.response.end(); } }
service class (service):
[operationcontract] [webinvoke(method = "post", uritemplate = "getalldrawsshort", bodystyle = webmessagebodystyle.wrappedrequest, responseformat = webmessageformat.json)] public list<drawshortobject> getalldrawsshort(string token, guid userid) { if (util.isauth(token)) { return new drawlogic().getalldrawsshort(userid); } else return null; }
jquery (client page):
function getalldrawsshort() { var jdata = {}; jdata.token = "123"; jdata.userid = "2f9e15d9-3654-4a43-89f4-07fea98a146f"; return $.ajax({ cache: false, type: "post", async: true, url: address + "getalldrawsshort", data: json.stringify(jdata), contenttype: "application/json", datatype: "json" }); };
i had similar issue. solved problem using jsonp datatype , responding custom callback wrapped response.
first change ajax request datatype 'jsonp'.
then build service response:
string callback = httpcontext.current.request.querystring["callback"]; string data = callback + "(" + yourjsondata + ")"; httpcontext.current.response.write(data);
Comments
Post a Comment