jquery - json ,webservice "undefine error" -


 function authonticate() {                   $.ajax({         type: "post",     url: "http://lp18mobile.azurewebsites.net/lp18ws.asmx/authonticateuser",               contenttype: "application/jsonp; charset=utf-8",             data: {                "some data"             },                            datatype: "json",             success: function (msg) {                //success               }             },             error: function (msg) {                //error             },         });     } 

i have used jquery,json,html5,webservice develop application.

when run app throw

undefined error @ "xmlhttprequest cannot load http://lp18mobile.azurewebsites.net/lp18ws.asmx/authonticateuser.  origin http://'localhost:5733/..' not allowed access-control-allow-origin." 

why?

your code attempting make cors request, not ordinary post.

modern browsers allow ajax calls pages in same domain source html page.

in other words, whenever html page tries make ajax request not on same domain target url (in case, lp18mobile.azurewebsites.net:80), browser won't make call (as you'd expect). instead, try make cors request. bear in mind that, browser, localhost:80 , localhost:5733 (same host, different ports) are different domains.

cors request? that?

wikipedia says: cross-origin resource sharing (cors) mechanism allows web page make xmlhttprequests domain. such "cross-domain" requests otherwise forbidden web browsers, due same origin security policy.

to put shortly, perform cors request, browser:

  • will first send option request target url
    • that's how found out origin ... not allowed access-control-allow-origin.
  • and only if server response option contains adequate headers (access-control-allow-origin 1 of them) allow cors request, browser perform call (almost way if html page @ same domain).
    • if expected headers don't come, browser gives (like did you).

how make work?

if can't change web service or http server hosted: there nothing can other deploying html file in server located @ same domain of service (in scenario, lp18mobile.azurewebsites.net:80).

if can change web service: enable cors in asp.net web services, can add headers adding following line to source pages:

response.appendheader("access-control-allow-origin", "*"); 

but workaround started. should know more cors headers. more details in: using cors access asp.net services across domains

if can change http server of web service: check this link see how can on apache. click here learn how enable in iis7 or here make in iis6.

how jsonp?

to use jsonp in scenario, you'd have change service return information via get, can't post using jsonp. (this answer points hacks, that's going far, think.)

i don't have access http server or web service. no workarounds @ all, then?

there options, none of them clean workaround, really. set application (at same domain/port of html file) forwards every tcp/ip request lp18mobile.azurewebsites.net:80, , fool browser. or set mirror of web service in domain/port. see, becomes dirty point on.


Comments

Popular posts from this blog

linux - Does gcc have any options to add version info in ELF binary file? -

android - send complex objects as post php java -

charts - What graph/dashboard product is facebook using in Dashboard: PUE & WUE -