简体   繁体   English

JQuery.ajax():使用WCF服务时出现参数数据问题

[英]JQuery.ajax(): Issue with parameter data when consuming a WCF Service

I'm using JQuery to consume a WCF Service. 我正在使用JQuery消耗WCF服务。 Actually this works fine: 实际上,这很好:

var para = ' { "Parameter" : { "ID" : "5", "Name" : "Peter" } }'
$.ajax({
   type: "POST",
   contentType: "application/json",
   data: para,
   url: url
   success: success
});

But I don't want to pass the data parameter as String and I think it should be possible to pass ist as array in any way. 但是我不想将data参数作为String传递,我认为应该可以以任何方式将ist作为数组传递。 Like that: 像那样:

var para = { "Parameter" : { "ID" : 5, "Name" : "Peter" } }

But when I try this I'm getting an error. 但是,当我尝试这样做时,我得到了一个错误。 What I'm doing wrong? 我做错了什么?

Thanks 谢谢

var para = '{ "ID" : "5", "Name" : "Peter" }';
$.ajax({
   type: "POST",
   data: para,
   url: url
   success: success
});

If you format it like this you should be able to get the values as 如果您这样格式化,则应该可以将值获取为

$_POST will return array('ID' => '5', 'Name' => 'Peter');

but you can also access it by doing: 但您也可以通过以下方式访问它:

$_POST['ID'] and $_POST['Name']

Also you could make use of the jquery post function: 您也可以使用jquery post函数:

var para = '{ "ID" : "5", "Name" : "Peter" }';
$.post(
    url, 
    para
);

You can use JSON.stringify function from the json2.js . 您可以使用JSON.stringify函数从json2.js Then you ajax call will be 然后你的ajax电话将是

var para = { Parameter : { ID :5, Name : "Peter" } };
$.ajax({
   type: "POST",
   contentType: "application/json",
   data: JSON.stringify(para),
   url: url
   success: success
});

The usage of manual conversion to the JSON string is not good because of possible spatial characterless in the string which must be escaped (see http://www.json.org/ for details). 手动转换为JSON字符串的方法不好,因为字符串中可能存在必须转义的无字符字符(有关详细信息,请参见http://www.json.org/ )。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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