I'm using JQuery to consume a WCF Service. Actually this works fine:
我正在使用JQuery来使用WCF服务。实际上这很好用:
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. Like that:
但我不想将数据参数作为String传递,我认为应该可以以任何方式将ist作为数组传递。像那样:
var para = { "Parameter" : { "ID" : 5, "Name" : "Peter" } }
But when I try this I'm getting an error. What I'm doing wrong?
但是当我尝试这个时,我收到了一个错误。我做错了什么?
Thanks
谢谢
2 个解决方案
#1
1
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
);
#2
0
You can use JSON.stringify function from the json2.js. Then you ajax call will be
您可以使用json2.js中的JSON.stringify函数。然后你将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/)。
#1
1
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
);
#2
0
You can use JSON.stringify function from the json2.js. Then you ajax call will be
您可以使用json2.js中的JSON.stringify函数。然后你将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/)。