tags:

views:

67

answers:

4

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?

A: 

I'm not sure, but I'd be interested in knowing if adding this below OperationContract attribute would work:

[WebInvoke(Method="POST",BodyStyle=WebMessageBodyStyle.Wrapped,ResponseFormat=WebMessageFormat.Json)

Came across this link as well: Creating a Webservice Proxy with jQuery

Bryce Fischer
No, does not work. POST is the default. To enable GET I'd need to add the [WebGet] attribute.
newtogit
you had type: "POST" in there, so I assumed you were posting....
Bryce Fischer
I AM posting, but POST is the default for AJAX-enabled WCF Services. Only if you want to use GET you need to add the [WebGet] attribute.
newtogit
A: 

First of all you should verify that your method returns data in JSON format (ResponseFormat = WebMessageFormat.Json). The usage of [WebInvoke (Method = "POST", ResponseFormat = WebMessageFormat.Json)] as the additional attribute of TestMeWithParam will be enough. If you define the same information inside of the config file it will also work of cause.

You main problem should be solved with the following call:

BaseServiceCall("TestMeWithParam", JSON.stringify(17), successCB, errorCB);

or

BaseServiceCall("TestMeWithParam", "17", successCB, errorCB);

JSON.stringify(17) is equal to "17" (integers will be not changed during JSON encoding, but will be strings of cause).

If the input parameter is a class, then you should send JSON serialized data of the corresponding JavaScript object without of encoding of parameter name of WCF method (without 'lastId'). The parameter names will be used only if you call ASMX web service instead of WFC. See my this old answer to compare.

Oleg
Yes, it does return in JSON format (I could confirm that, now that it works). But even if it didn't the server breakpoint should have been hit. Calling without parameter name DOES NOT work. I tried it. With parameter name, it works.
newtogit
@newtogit: Sorry, I am not sure that I understand you correct. Does the call `BaseServiceCall("TestMeWithParam", "17", successCB, errorCB)` work or not? Which call "without parameter name DOES NOT work" and which works?
Oleg
Supplying only the value (and not the parameter name) does not work for me. If I supply both (as in the answer I posted myself), it works.
newtogit
A: 

i had similar problems when I tried to use jquery with wcf! try to change this:

"{'lastId':'17'}" to '{"lastId":"17"}'
//i know you tried something similar but try putting the brackets around the number as well

let me know if it doesn't work, i will edit my post to go through the solution with you.

p.s. you should also try @Bryce's suggestion!

[WebInvoke(Method = "POST",
           BodyStyle = WebMessageBodyStyle.Wrapped,
           ResponseFormat = WebMessageFormat.Json,
           RequestFormat = WebMessageFormat.Json)]
//note the response and request format
Mouhannad
I changed the quoting as you suggested. It least it seems to have been part of the solution. It also works without the WebInvoke stuff.
newtogit
A: 

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)

[AspNetCompatibilityRequirements(RequirementsMode = AspNetCompatibilityRequirementsMode.Allowed)]
public class MyService
{
    [OperationContract]
    public int TestMeWithParam(int lastId)
    {
        return lastId;
    }

    [OperationContract]
    public int TestMe()
    {
        return 5;
    }
}

As I have read somewhere else, no WebInvoke is needed at all. Just the plain standard.

Client 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: 200000,
    success: successCB,
    error: errorCB
});
}

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. :(

newtogit
I'm glad I could help. I had the same issue with the quotes myself recently!
Mouhannad