newtogit
newtogit

Reputation: 357

Calling WCF service with jquery and parameters

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?

Upvotes: 2

Views: 11336

Answers (4)

newtogit
newtogit

Reputation: 357

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

Upvotes: 0

Oleg
Oleg

Reputation: 222007

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.

Upvotes: 0

Mouhannad
Mouhannad

Reputation: 2209

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

Upvotes: 3

Bryce Fischer
Bryce Fischer

Reputation: 5432

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

Upvotes: 0

Related Questions