在Visual Studio 2010中我創建了一個新的啓用Ajax的WCF服務jQuery的WCF服務MVC2 VS2010 .NET 4.0調用與參數失敗
[ServiceContract(Namespace = "TestWCFAjax.Bridge")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class Bridge
{
[OperationContract]
public string DoWork()
{
return "jQuery WCF call without parameters from MVC2 works." ;
}
[OperationContract]
public string DoWork1(string parm)
{
return parm + " jQuery WCF call with parameters from MVC2 fails";
}
在主控制器的Index.aspx視圖我添加了jQuery:
功能CallWebMethod(){
$.ajax(
{
type: "POST",
contentType: "application/json; charset-utf-8",
url: "http://localhost:1452/Bridge.svc/DoWork1",
dataType: "json",
data: '{"parm":"test"}',
error: jqueryError,
success: function (msg) {
alert("back");
var divForResult = document.getElementById("test");
divForResult.innerHTML = "Result: <b>" + msg.d + "</b>";
}
})
}
function jqueryError(request, status, error) {
alert(request.responseText + " " + status + " " + error);
}
(使用VS 2010內置的Web服務器)
當我打電話給DoWork時,它工作正常。 當我調用DoWork1時,它總是返回「錯誤未定義」,並且WCF調用從不發生。
我試過的每個組合: [WebInvoke(方法= 「POST」,BodyStyle = WebMessageBodyStyle.WrappedRequest,ResponseFormat = WebMessageFormat.Json,RequestFormat = WebMessageFormat.Json) 我能想到的,它不幫幫我。
我必須缺少一些簡單的東西。
有很多關於如何使這項工作,以及「無參數」版本,沒有人爲我工作。
任何人都可以發佈樣本MVC2 jQuery 1.4 .NET 4.0 WCF VS2010工作示例或發現可能的錯誤?
謝謝。
你是如何在wcf config上配置你的基地址的? Bridge.svc是否包含在內?這是強制性的嗎? Tks – Pascal 2010-12-17 13:43:08