views:

1027

answers:

1

Hi,

I have configured the web service to use Json as described on this blog: http://www.west-wind.com/weblog/posts/164419.aspx and various other blogs, but I couldn't create a client to consume this service. I tried various things, but invariably I got meaningless exceptions. What is the correct way to implement the (WCF I should add) client?

A: 

What are the exceptions? They may be meaningless to you, but some one around here might find them helpful in diagnosing your problem. I use jQuery to make ajax request to a WCF service and the settup usually looks like this:

     $(document).ready(function() {

        $.ajaxSetup({
            type: "POST",
            processData: true,
            contentType: "application/json",
            timeout: 5000,
            dataType: "json"
        });
        var data = { "value": 5 };

        AjaxPost("GetData", data, OnEndGetData, OnError);
    });

    function OnEndGetData(result) {
        alert(result.GetDataResult);
    }

    function OnError(msg) {
        alert(msg);
    }

function AjaxPost(method, data, callback, error) {
    var stringData = JSON.stringify(data);
    var url = "Service1.svc/" + method;

    $.ajax({
        url: url,
        data: stringData,
        success: function(msg) {
            callback(msg);
        },
        error: error
    });
}

The JSON.stringify() can be found in the json.org script: http://www.json.org/js.html, and my sig for GetData method looks like this:

[OperationContract]
[WebInvoke(Method = "POST", BodyStyle = WebMessageBodyStyle.Wrapped, ResponseFormat =   WebMessageFormat.Json)]
string GetData(int value);
Mike_G
Hi, maybe I was not clear enough, but I was really trying to implement a WCF client inside a winform appication in C#, and not a web client.
Grzenio
And also I get an error for the line `$.ajax({` : $ is not defined
Grzenio
Im not sure you can implement a JSON client without using a plain old WebClient or HttpRequest/Response setup.The $ is part of jQuery.
Mike_G