2013-05-02 34 views
0

我正在開發兩個站點。其中一個我有頁面Service.aspx,有一些WebMethod。我想要從其他站點訪問它們。跨站點Ajax WebService或WebMethod

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; 
} 

之後,當我用我的劇本,我有錯誤:

XmlHttpRequest error: Origin null is not allowed by Access-Control-Allow-Origin

然後我嘗試解決這個問題。我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=&quot;Data Source=WIN-V5EHKO65FPU\SQLEXPRESS;Initial Catalog=C:\INETPUB\ACMTESTSYSTEM\INTERFACE\APP_DATA\TESTSYSTEM.MDF;Integrated Security=True;MultipleActiveResultSets=True&quot;" providerName="System.Data.EntityClient" /> 
    </connectionStrings> 
</configuration> 

之後,我嘗試使用Global.asax

protected void Application_BeginRequest(object sender, EventArgs e) 
{ 
    HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*"); 
} 

然後我嘗試使用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); 
     } 
    } 
} 

在我的劇本也許問題?

1:

var dataURL = "http://95.31.32.69/WebService.asmx/CheckUserLogin?login=1"; 
$.getJSON(dataURL+"&jsoncallback=?",myCallback); 
function myCallback(data){  
alert(data); 
} 

返回錯誤:

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; 
} 

返回錯誤:

XMLHttpRequest cannot load http://95.31.32.69/WebService.asmx/CheckUserLogin . Origin null is not allowed by Access-Control-Allow-Origin.

3:

var url = 'http://95.31.32.69/WebService.asmx/CheckUserLogin?login=1&callback=?'; 
    $.get(url, function (data) { 
    alert(data); 
    }); 

返回錯誤:

XMLHttpRequest cannot load http://95.31.32.69/WebService.asmx/CheckUserLogin . Origin null is not allowed by Access-Control-Allow-Origin.

4:

var dataURL = "http://95.31.32.69/WebService.asmx/Foo"; 
    $.getJSON(dataURL+"&jsoncallback=?",myCallback); 
    function myCallback(data){  
    alert(data); 
    } 

返回錯誤:

Failed to load resource: the server responded with a status of 400 (Bad Request)

等等。該網站工作在IIS 7

回答

1

AJAX調用只能在同一原始服務器即同源-政策由於安全原因作出的。使用其他方法(如JSONP)來處理跨域請求。因此,錯誤Origin null is not allowed by Access-Control-Allow-Origin.

Same-Origin Policy

+0

我想,我嘗試使用它們,但它不工作。 JSONP上面有很多方法可以解決這個問題,但是我想要代碼。例如:'var d ='{「login」:「1」}'; $ .ajax({url:「http://95.31.32.69/WebService。asmx/CheckUserLogin「, contentType:」application/json;字符集= UTF-8" , 數據:d, 數據類型: 「JSONP」, 成功:功能(數據,textStatus,XMLHttpRequest的){ 警報(數據); } });' – lordrp 2013-05-02 14:01:33