jQuery WCF Service MVC2 VS2010 .NET 4.0 call with parameters fails
Posted
by AUSTX_RJL
on Stack Overflow
See other posts from Stack Overflow
or by AUSTX_RJL
Published on 2010-04-22T14:36:03Z
Indexed on
2010/04/22
22:33 UTC
Read the original article
Hit count: 912
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.
© Stack Overflow or respective owner