我正在嘗試做一些我認爲很簡單的事情。我需要創建一個可以通過JQuery發佈的WCF服務。我在被定義爲如下我的WCF服務的操作:WCF - 「遇到意外字符'c'。」
[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, RequestFormat=WebMessageFormat.Json, ResponseFormat=WebMessageFormat.Json)]
public string SendMessage(string message, int urgency)
{
try
{
// Do stuff
return "1"; // 1 represents success
}
catch (Exception)
{
return "0";
}
}
然後我嘗試通過JQuery從ASP.NET頁面訪問此操作。我的JQuery代碼來訪問此操作如下所示:
function sendMessage(message) {
$.ajax({
url: "/resources/services/myService.svc/SendMessage",
type: "POST",
contentType: "application/json; charset=utf-8",
data: ({ message: message, urgency: '1' }),
dataType: "json",
success: function (data) {
alert("here!");
},
error: function (req, msg, obj) {
alert("error: " + req.responseText);
}
});
}
當我執行此腳本時,錯誤處理程序被觸發。其中,我收到一條錯誤消息:
「遇到意外字符'c'。」
此消息包含在一個長堆棧跟蹤中。我的問題是,我做錯了什麼?我收到了其他的帖子,比如這個(How to post an array of complex objects with JSON, jQuery to ASP.NET MVC Controller?),沒有任何運氣。我如何獲得這種基本的互動工作?
謝謝!
我認爲你傳遞的參數不正確。看看下面的頁面:[在ASP.NET AJAX中使用jQuery時要避免的三個錯誤](http://encosia.com/2008/06/05/3-mistakes-to-avoid-when-using-jquery -with-aspnet-ajax /) – Giorgi 2010-03-20 19:29:58
目前我無法訪問該網站 - 它說了什麼? – 2012-06-27 11:40:18