Ok, this is one of these basic questions, but I’ve googled and debugged now for two hours and the error escapes me.
Simple scenario: WCF service with methods with parameters which I’d like to call through jquery. I can call methods without params alright, but with params, the call never makes it to my breakpoint in .NET.
ServerCode:
[ServiceContract(Namespace = "http://www.myhost.de")]
[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
[OperationContract]
public int TestMeWithParam(int lastId)
{
return lastId;
}
[OperationContract]
public int TestMe()
{
return 5;
}
}
Javascript code
function BaseServiceCall(serviceName, dataInput, successCB, errorCB) {
$.ajax({
type: "POST",
contentType: "application/json; charset=utf-8",
url: BaseUrl + "Services/MyService.svc/" + serviceName,
data: dataInput,
dataType: "json",
timeout: 2000,
success: successCB,
error: errorCB
});
}
function ServiceGetMessages(lastMessageId, successCB, errorCB) {
BaseServiceCall("TestMeWithParam", "{'lastId':'17'}", successCB, errorCB);
//BaseServiceCall("TestMe", "", successCB, errorCB);
}
So, if I call the TestMe service it returns 5. It works. TestMeWithParam never gets called.
What’s going on?
So, now it works. I am not very sure why, since I didn’t change a lot. I fear one factor was that my timeout was too small for debugging (but even then it should have worked).
So, now the server code which works for me (with and without params)
As I have read somewhere else, no WebInvoke is needed at all. Just the plain standard.
Client code:
function ServiceGetMessages(lastMessageId, successCB, errorCB) {
BaseServiceCall(“TestMeWithParam”, ‘{“lastId”:”‘ + lastMessageId + ‘”}’, successCB, errorCB);
//BaseServiceCall(“TestMe”, ‘””‘, successCB, errorCB);
}
I changed the quotation marks as suggested by Mouhannad, even though I am sure that I tried that before.
I also tried without the “lastId” which did not work.
I had this experience before with WCF: you play around and play around, then it works and you are not sure why. 🙁