JQuery WCF Service MVC2 VS2010.NET 4.0 call with parameters doesn't work
In Visual Studio 2010 I created a new WCF service with Ajax support
[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 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: [WebInvoke (method = "POST", BodyStyle = WebMessageBodyStyle.WrappedRequest, ResponseFormat = WebMessageFormat.Json, RequestFormat = WebMessageFormat.Json)] I can think of it and it doesn't help.
I need to skip something simple.
There are many posts on how to do this, and other than the "no parameters" version, none of them worked for me.
Can anyone post an MVC2 jQuery 1.4.NET 4.0 WCF VS2010 sample or identify the likely error?
Thanks.
a source to share
I wrote and presented using jQuery with WCF . All MVC doesn't really matter, or shouldn't matter, at least :) I have a few posts about using it and there is a link for you to download the latest source from my demo project.
a source to share