how to call wcf restful service from fiddler by JSON request?

后端 未结 2 1312

I am new in wcf restful service. I couldn’t find the problem which was, why my wcf restful service give ‘bad request’. I use .NET 4.0.

My service is:

2条回答
  •  眼角桃花
    2021-02-09 07:15

    One thing you are doing wrong is the format of the request object. Chang it to:

    { n1: { "Number1":"7","Number2":"7"} }
    

    The parameter names are the root property names of the json object passed to a wcf json endpoint.

    I have a sample wcf service on git. Amongst other things, it has a json endpoint and in the Default.asmx I use jquery to make a call to it. I would suggest you construct a similar webpage (host it on the same website as the WCF service) that calls your json service via jquery and test the service with that while fiddler is running to get a sample request. This will be easier and less error prone than constructing the request yourself since jquery will take care of lots of details in the header.

    The sample jquery ajax code for calling my echo service that you can adapt for your service is as follows:

        $("#btnEchoString").click(function () {
            var request = $("#request");
            var response = $("#response");
            $.ajax({
                url: 'EchoService.svc/JSON/Echo',
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify({ request: request.val() }),
                dataType: "json",
                success: function (data) {
                    response.val(data.d);
                },
                error: function (request, status, error) {
                    //TODO: do something here
                }
            });
        });
    

提交回复
热议问题