简体   繁体   中英

Passing json paramter to wcf rest service post method

I am completely new to WCF world. I am trying to make POST call to WCF Service method and passing json data to it.
My code snippet is as below:

Ajax POST call:

function PostJSON() {

        var form_data = {
           "req_limitmsg_header": {
                "brch_code": "784",
                "rqst_id": "00129538",
                "tnx_id": "20150200008695",
                "inp_user_id": "UAT01",
                "inp_dttm": "20150311183413",
                "Func_code": "6010 ",
                "idms_tnx_type_code": "N",
                "Spaces": "12",
                "prod_ref_id": "12"
            } ,
            "req_limitmsg_detail": [
                {
                   "jrn_exc_cur_code": "0000",
                    "jrn_exc_act_no": "019000090000",
                    "sign of exc_acpt_amt": "+",
                    "exc_acpt_amt": "0000000000000000",
                    "sign of jrn_exc_amt": "+",
                    "jrn_exc_amt": "0000000001500000"
                },
                {
                    "jrn_exc_cur_code": "0000",
                    "jrn_exc_act_no": "019000090000",
                    "sign of exc_acpt_amt": "+",
                    "exc_acpt_amt": "0000000000000000",
                    "sign of jrn_exc_amt": "+",
                    "jrn_exc_amt": "0000000001500000"
                }
            ]
        };

        $.ajax({
            cache: 'false',
            async: 'false',
            type: 'POST',
            url: "http://localhost:10647/JsonTest.svc/Rest/RequestLimitCheckService",
            data: JSON.stringify(form_data),
            contentType: "application/json; charset=utf-8",
            dataType: 'json',
            crossDomain: true,
            processData: true,
            success: function (data, status, jqXHR) {
                if(data != null)
                {
                    alert("NOT NULL");
                    alert(data.toString());
                }
                else
                {
                    alert("NULL");
                    alert(data.toString());
                    alert(status.toString());
                }
            },
            error: function (response) {
                alert(response);
            }
        });
    }

WCF Method:

 [WebInvoke(Method = "POST",  ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,
            UriTemplate = "RequestLimitCheckService")]
        public string RequestLimitCheck(string form_data)
        {
//do somethig
}

WCF Interface

  [OperationContract]
  string RequestLimitCheck(string form_data);

What is happening is I am always getting null string. I tried specifying query string parameter style as well but it's of no use. It is only working fine if I specify class to accept the json data. Please let me know if I am doing anything wrong or missing something.

Remove JSON.stringify(form_data) and use simply form_data instead and try again. Also you do need to specify the type of request format you are expecting.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM