0
我有ajax查詢問題,我嘗試使用wcf服務的後期查詢,這不起作用。請幫幫我。WCF + Ajax查詢
我的服務,在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();
}
}
服務合同
[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代碼
[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);
}
}
和我QUER下一個代碼Ÿ
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) {
}
});
}
whay它不工作,我不knowe。 P.S.對不起我的英語不好。
嘗試'數據類型: 「JSONP」,'的[跨域AJAX不發送X-要求,隨着頭] – Grundy 2014-09-30 07:48:36
可能重複(http://stackoverflow.com/questions/8163703/cross- domain-ajax-doesnt-send-x-requested-with-header) – Grundy 2014-09-30 07:54:36
jsonp不起作用。 – Voucik 2014-09-30 08:49:24