问题
In Visual Studio 2010 I created a new Ajax enabled WCF Service
[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";
}
In the Home Controllers Index.aspx view I add the jQuery:
function 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);
}
(using the built-in Web Server in VS 2010)
When I call DoWork, it works fine. When I call DoWork1 it always returns "error undefined" and the WCF call never happens.
I've tried every combination of: [WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] I can think of and it does not help.
I must be missing something simple.
There are MANY posting about how to make this work, and other than the "no parameter" version, none have worked for me.
Can anyone post a sample MVC2 jQuery 1.4 .NET 4.0 WCF VS2010 working sample or spot the likely error?
Thanks.
回答1:
contentType: "application/json; charset-utf-8",
should be
contentType: "application/json; charset=utf-8",
回答2:
I have been writing and presenting on using jQuery with WCF. The whole MVC does not really matter, or shouldn't matter at least :) I have several post about using it and there is a link for you to download the lastest source from my demo project.
来源:https://stackoverflow.com/questions/2691745/jquery-wcf-service-mvc2-vs2010-net-4-0-call-with-parameters-fails