请高手指点:
[WebInvoke(UriTemplate = "/create", Method = "GET", ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)]
[OperationContract]
Case Create(Case entity);
我要定义这样一个服务,但是呢我想传入一个Case对象作为参数,那么我的UriTemplate 该如何写?? 可以用GET方式提交一个对象Case 数据??
请大侠指点!!!
当我用POST方式,那我又该怎么用JS提交数据???
function postData() {
var entity = [{
entity: {
"CaseID": "11fdc0af-ed4f-e211-9ded-1cc1de6daa3e",
"CaseNo": "CAS-01000-D2F6P6",
"CaseOrigin": 3,
"CaseType": 3,
"Customer": {
"ID": "fdfcc0af-ed4f-e211-9ded-1cc1de6daa3e",
"LogicalName": "account",
"Name": "Unusual Store (sample)"
},
"Owner": {
"ID": "70384c3d-9f60-4646-9424-ce0b7171e8d4",
"LogicalName": "systemuser",
"Name": "Shan Tan"
},
"Priority": 3,
"Status": 1,
"Title": "Contact information required (sample)"
}
}];
var url = "http://localhost:18083/Services/CaseManagementService.svc/create";
$.ajax({
type: "post",
url: url,
// contentType: "application/json;charset=UTF-8",
data: entity,
dataType: "json"
}).complete(function (complete) {
console.dir(complete);
});
}
这是js POST的代码,但是呢,总是400 错误,当我去掉data参数后,服务端有响应,但是接收不到我传入的参数,问题:该如何传入参数???
自己搞定了:
在Global.asax 加入一下代码,即可
protected void Application_BeginRequest(object sender, EventArgs e)
{
HttpContext.Current.Response.Cache.SetCacheability(HttpCacheability.NoCache);
HttpContext.Current.Response.Cache.SetNoStore();
EnableCrossDmainAjaxCall();
}
private void EnableCrossDmainAjaxCall()
{
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();
}
}
js:
function postData() {
var caseEntity = {};
caseEntity.entity = {
"CaseID": "11fdc0af-ed4f-e211-9ded-1cc1de6daa3e",
"CaseNo": "CAS-01000-D2F6P6",
"CaseOrigin": 3,
"CaseType": 3,
"Customer": {
"ID": "fdfcc0af-ed4f-e211-9ded-1cc1de6daa3e",
"LogicalName": "account",
"Name": "Unusual Store (sample)"
},
"Owner": {
"ID": "70384c3d-9f60-4646-9424-ce0b7171e8d4",
"LogicalName": "systemuser",
"Name": "Shan Tan"
},
"Priority": 3,
"Status": 1,
"Title": "Contact information required (sample)"
};
var url = "http://localhost:18083/Services/CaseManagementService.svc/create";
$.ajax({
cache: false,
type: "POST",
async: false,
url: "http://localhost:18083/Services/CaseManagementService.svc/create",
data: JSON.stringify(caseEntity),
contentType: "application/json",
dataType: "json",
success: function (Case) {
alert(Case.Title);
},
error: function (xhr) {
alert(xhr.responseText);
}
});
}
@吴哥-Angkor:
More details:
http://www.codeproject.com/Articles/128478/Consuming-WCF-REST-Services-Using-jQuery-AJAX-Call
@吴哥-Angkor:
http://www.codeproject.com/Articles/59551/Consuming-a-WCF-ASMX-REST-Service-using-jQuery