web api 解決Ajax請求跨域問題

2022-08-27 06:39:09 字數 1089 閱讀 9933

//預設的建構函式允許 ""網域名稱下呼叫介面

public crosssiteattribute()

public crosssiteattribute(string originheader )

}private const string origin = "origin";

/// /// access-control-allow-origin是html5中定義的一種伺服器端返回response header,用來解決資源(比如字型)的跨域許可權問題。

///

private const string accesscontrolalloworigin = "access-control-allow-origin" ;

/// /// originheaderdefault的值可以使 url 或 *,如果是 url 則只會允許來自該 url 的請求,* 則允許任何域的請求

///

/// /// 該方法允許api支援跨域呼叫

///

/// 初始化 system.web.http.filters.httpactionexecutedcontext 類的新例項。

}

### 呼叫很簡單
[httppost]

[crosssite("")]

public response removepc(model model)

### 還有一種方式 ,就是直接在web.config裡新增節點 的方式解決,就是在  節點下新增。

webapi 解決ajax跨域請求問題

false true access control allow origin value access control allow headers value content type access control allow methods value get,post,put,delete,op...

解決ajax跨域請求 (總結)

ajax跨域請求,目前已用幾種方法實現 1 用原生js的xhr物件實現。var url 建立xhr物件 function createcorsxhr url,method else if typeof xdomainrequest undefined else xhr null return xhr...

解決ajax跨域請求 (總結)

ajax跨域請求,目前已用幾種方法實現 1 用原生js的xhr物件實現。var url 建立xhr物件 function createcorsxhr url,method else if typeof xdomainrequest undefined else xhr null return xhr...