出於某種原因,我只能在使用jQuery ajax時將包含數字的字符串傳遞給我的Web服務。到目前爲止,這並不是一個問題,因爲我總是將ID傳遞給我的wcf服務。但我現在試圖做更復雜的事情,但我無法弄清楚。使用jquery ajax將字符串發送到wcf服務。爲什麼我只能發送一串數字?
在我的界面:
[OperationContract]
[WebInvoke(ResponseFormat = WebMessageFormat.Json)]
DataTableOutput GetDataTableOutput(string json);
我的web服務:
public DataTableOutput GetDataTableOutput(string json)
{
DataTableOutput x = new DataTableOutput();
x.iTotalDisplayRecords = 9;
x.iTotalRecords = 50;
x.sColumns = "1";
x.sEcho = "1";
x.aaData = null;
return x;
}
使用Javascript/jQuery的:
var x = "1";
$.ajax({
type: "POST",
async: false,
url: "Services/Service1.svc/GetDataTableOutput",
contentType: "application/json; charset=utf-8",
data: x,
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.responseText);
}
});
上面的代碼完美的作品。但是,當我將x更改爲「t」或甚至更改爲「{'test':'test'}」我在Firebug中遇到錯誤400錯誤請求錯誤。
感謝, 約翰
編輯: 取得一些進展! data:JSON.stringify(「{'test':'test'}」), 將字符串發送到我的函數!
EDIT2:
var jsonAOData = JSON.stringify(aoData);
$.ajax({
type: "POST",
async: false,
url: sSource,
contentType: "application/json; charset=utf-8",
data: "{'Input':" + jsonAOData + "}",
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.responseText);
}
});
EDIT3:我修改的代碼塊我把EDIT2向上的上方。
交換的「和‘!沒有的伎倆
$.ajax({
type: "POST",
async: false,
url: sSource,
contentType: "application/json; charset=utf-8",
data: '{"Input":' + jsonAOData + '}',
dataType: "json",
success: function (msg) {
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//alert(XMLHttpRequest.status);
//alert(XMLHttpRequest.responseText);
}
});
不過,我有一個新的問題:
public DataTableOutput GetDataTableOutput(DataTableInputOverview Input)
{
這裏的輸入完全是零,我從jsonAOData傳遞的價值沒’ t分配給DataTableInputOverview輸入變量:(
你可以嘗試設置X =「{‘測試’:‘測試’}」? – JoshRivers 2011-03-11 20:29:00
也沒有工作:( – Robodude 2011-03-11 20:34:20
JSON.stringify是我的下一個想法,你可以直接使用你的對象,像http://jsfiddle.net/nzNxv/? – JoshRivers 2011-03-11 21:19:25