0
服務方法看起來像這樣WebGet JSON複雜的參數
public class UploadItem
{
public string fileName { get; set; }
public string fileDesc { get; set; }
}
[OperationContract, WebGet]
public int WriteUploadItem(UploadItem uploadItem)
{
//implementation
}
調用看起來像這樣
var data = {
fileName: self.fileName(),
fileDesc: self.fileDesc(),
};
$.ajax({
url: "Fx.svc/WriteUploadItem",
data: { uploadItem: data },
success: function (result) {
//implementation
},
error: function (result) { alert($.parseJSON(result.responseText).Message); }
});
產生此
GET http://localhost:49701/Fx.svc/WriteUploadItem?uploadItem%5BfileName%5D
=2014-01-21.gif&uploadItem%5BfileDesc%5D=adesgfA HTTP/1.1
X-Requested-With: XMLHttpRequest
Accept: */*
Referer: http://localhost:49701/index.html#upload-queue
Accept-Language: en-AU,en;q=0.5
Accept-Encoding: gzip, deflate
User-Agent: Mozilla/5.0 (Windows NT 6.3; WOW64; Trident/7.0; rv:11.0) like Gecko
Host: localhost:49701
DNT: 1
Connection: Keep-Alive
那些URL編碼的參數是這樣的,當解碼
uploadItem[fileName]=2014-01-21.gif&uploadItem[fileDesc]=adesgfA
WebGet方法被調用,但參數爲空。
我成功通過一個單一的參數來的另一種方法這樣
var userId = "73c2e254-5440-45eb-9099-58fa08dd037b"; // me.UserID();
$.ajax({
url: "Fx.svc/UserFiles",
data: { userId: userId },
success: function (result) {
//implementation
},
error: function (result) { alert($.parseJSON(result.responseText).Message); }
});
據我可以看到,唯一的區別是該參數的值是不同的。最初有更多的領域,但我刪除了更麻煩的數據類型,並且很希望發現即使使用簡單的字符串也會出現問題。
有什麼不對?我該怎麼辦?是否有某種屬性需要放在C#UploadItem類上?