C# 将json参数传递给wcf rest服务post方法
我对WCF世界完全陌生。 我正在尝试对WCF服务方法进行POST调用,并将json数据传递给它。C# 将json参数传递给wcf rest服务post方法,c#,ajax,json,wcf,rest,C#,Ajax,Json,Wcf,Rest,我对WCF世界完全陌生。 我正在尝试对WCF服务方法进行POST调用,并将json数据传递给它。 我的代码片段如下所示: Ajax POST调用: function PostJSON() { var form_data = { "req_limitmsg_header": { "brch_code": "784", "rqst_id": "00129538",
我的代码片段如下所示: Ajax POST调用:
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);
}
});
}
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "RequestLimitCheckService")]
public string RequestLimitCheck(string form_data)
{
//do somethig
}
WCF方法:
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);
}
});
}
[WebInvoke(Method = "POST", ResponseFormat = WebMessageFormat.Json,RequestFormat=WebMessageFormat.Json,BodyStyle = WebMessageBodyStyle.Wrapped,
UriTemplate = "RequestLimitCheckService")]
public string RequestLimitCheck(string form_data)
{
//do somethig
}
WCF接口
[OperationContract]
string RequestLimitCheck(string form_data);
发生的事情是我总是得到空字符串。我也尝试过指定查询字符串参数样式,但没有用。只有指定类来接受json数据,它才能正常工作。
如果我做错了什么或遗漏了什么,请告诉我 删除JSON.stringify(form_数据),改用form_数据,然后重试。此外,您还需要指定所需的请求格式类型。@Sandeep我尝试删除JSON.stringify(form_数据)并仅使用form_数据,但它甚至没有命中WCF方法。另外,我在WCF方法中提到了请求格式的类型(请参阅上面的代码片段)尽管如此,使用上面的参考资料作为示例代码仍然不起作用,因为示例代码试图实现与您类似的功能。希望这会有所帮助。