javascript - Cross-site Ajax WebService or WebMethod -
i developing 2 sites. in 1 of them have page service.aspx
, have webmethod
. want have access them other site.
service.aspx
:
namespace interface { public partial class service : system.web.ui.page { [webmethod] public static bool checkuserlogin(string login) { bl.userbl logic = new bl.userbl(); return logic.isunique(login); } } }
script.js
:
function isgoodlogin() { var login = $("[id$='tbsignuplogin']").val(); var data = '{"login":"' + login + '"}'; var flag = false; $.ajax({ type: "post", url: "http://95.31.32.69/service.aspx/checkuserlogin", async: false, data: data, contenttype: "application/json; charset=utf-8", datatype: "json", success: function (msg) { if (msg.d == true) { flag = true; } }, error: function (xhr, status, error) { handleexception(xhr.responsetext) } }); return flag; }
after that, when use script, have error:
xmlhttprequest error: origin null not allowed access-control-allow-origin
then try solve problem. web.config
:
<configuration> <system.web> <compilation debug="true" targetframework="4.0" /> <webservices> <protocols> <add name="httpget"/> <add name="httppost"/> </protocols> </webservices> </system.web> <system.webserver> <httpprotocol> <customheaders> <add name="access-control-allow-origin" value="*" /> <add name="access-control-allow-headers" value="content-type" /> </customheaders> </httpprotocol> </system.webserver> <connectionstrings> <add name="testsystementities" connectionstring="metadata=res://*/model1.csdl|res://*/model1.ssdl|res://*/model1.msl;provider=system.data.sqlclient;provider connection string="data source=win-v5ehko65fpu\sqlexpress;initial catalog=c:\inetpub\acmtestsystem\interface\app_data\testsystem.mdf;integrated security=true;multipleactiveresultsets=true"" providername="system.data.entityclient" /> </connectionstrings> </configuration>
after try use global.asax
:
protected void application_beginrequest(object sender, eventargs e) { httpcontext.current.response.addheader("access-control-allow-origin", "*"); }
then try use webservice.asmx
:
namespace interface { [system.web.script.services.scriptservice] public class webservice : system.web.services.webservice { [webmethod] public bool checkuserlogin(string login) { bl.userbl logic = new bl.userbl(); return logic.isunique(login); } [webmethod] [scriptmethod(usehttpget = true, responseformat = responseformat.json)] public string foo() { var json = new javascriptserializer().serialize(new { prop1 = "some property", }); string jsoncallback = httpcontext.current.request["jsoncallback"]; return string.format("{0}({1})", jsoncallback, json); } } }
maybe problem in scripts?
1:
var dataurl = "http://95.31.32.69/webservice.asmx/checkuserlogin?login=1"; $.getjson(dataurl+"&jsoncallback=?",mycallback); function mycallback(data){ alert(data); }
returns error:
uncaught syntaxerror: unexpected token <
2:
function isgoodlogin2() { var login = $("[id$='tbsignuplogin']").val(); var data = '{"login":"' + login + '"}'; var flag = false; $.ajax({ type: "post", url: "http://95.31.32.69/webservice.asmx/checkuserlogin", async: false, data: data, contenttype: "application/json; charset=utf-8", datatype: "json", success: function (msg) { if (msg.d == true) { flag = true; } }, error: function (xhr, status, error) { handleexception(xhr.responsetext) } }); return flag; }
returns error:
xmlhttprequest cannot load
http://95.31.32.69/webservice.asmx/checkuserlogin
. origin null not allowed access-control-allow-origin.
3:
var url = 'http://95.31.32.69/webservice.asmx/checkuserlogin?login=1&callback=?'; $.get(url, function (data) { alert(data); });
returns error:
xmlhttprequest cannot load
http://95.31.32.69/webservice.asmx/checkuserlogin
. origin null not allowed access-control-allow-origin.
4:
var dataurl = "http://95.31.32.69/webservice.asmx/foo"; $.getjson(dataurl+"&jsoncallback=?",mycallback); function mycallback(data){ alert(data); }
returns error:
failed load resource: server responded status of 400 (bad request)
and many others. web site works on iis 7.
ajax calls can made on same originating server i.e. same-origin-policy due security reasons. use other methods such jsonp cross-domain requests. hence error origin null not allowed access-control-allow-origin.
Comments
Post a Comment