WCF + Ajax,Access-Control-Allow-Origin [英] WCF + Ajax, Access-Control-Allow-Origin
问题描述
我有ajax查询的问题,我尝试为wcf服务进行帖子查询,这不起作用。
我的浏览器出现下一个错误:
< b> xmlhttprequest无法加载myUrl请求头字段是access-control-allow-headers不允许的
这里我的项目(在github上)
我的服务,在global.asax中有下一个代码
I have problem with ajax query, i try make post query for wcf service and this don't work.
I got next error in my browser:
xmlhttprequest cannot load "myUrl" request header field is not allowed by access-control-allow-headers
Here my project (on github)
My Service, have next code in global.asax
protected void Application_BeginRequest(object sender, EventArgs e)
{
EnableCrossDomainAjaxCall();
}
private void EnableCrossDomainAjaxCall()
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");
if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
{
HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST");
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 contract
[ServiceContract]
public interface IService1
{
[OperationContract()]
[WebInvoke(Method = "GET", UriTemplate = "/GetTestData", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
List<Data.Test> GetTestData();
[OperationContract]
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
void AddTestData(Data.Test value);
}
和svc代码
And svc code
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Service1 : IService1
{
//static data for test
private static List<Data.Test> _data = new List<Data.Test>() { new Data.Test() { ID = 0, Name = "a" }, new Data.Test() { ID = 1, Name = "B" } };
public List<Data.Test> GetTestData()
{
return _data;
}
public void AddTestData(Data.Test value)
{
_data.Add(value);
}
}
和我的查询
and my query
function SendData() {
var Json = { 'id': $("#id").val(), 'name': $("#name").val() };
$.ajax({
type: "Post",
url: "http://localhost:10468/Service1.svc/AddTestData",
data: JSON.stringify(Json),
contentType: "application/json;charset-uf8",
dataType: "json",
success: function (msg) {
},
error: function (err) {
}
});
}
我不知道它不起作用。请帮帮我。
P.S.抱歉,我的英文。
I don't knowe whay it's not working. Help me please.
P.S. Sorry for my english.
推荐答案
( #id )。val(),' name':
("#id").val(), 'name':
( #name)。val()};
("#name").val() };
.ajax({
type: Post,
url: http:// localhost:10468 / Service1.svc / AddTestData,
data: JSON .stringify(Json),
contentType: application / json; charset-uf8,
dataType: json ,
成功:功能(msg){
},
错误:功能(错误){
}
});
}
.ajax({ type: "Post", url: "http://localhost:10468/Service1.svc/AddTestData", data: JSON.stringify(Json), contentType: "application/json;charset-uf8", dataType: "json", success: function (msg) { }, error: function (err) { } }); }
我不知道它不起作用。请帮帮我。
P.S.对不起我的英文。
I don't knowe whay it's not working. Help me please.
P.S. Sorry for my english.
这篇关于WCF + Ajax,Access-Control-Allow-Origin的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!