使用 Jquery Ajax 将对象作为数据发布
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/1068189/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Post an object as data using Jquery Ajax
提问by RubbleFord
My code that I tried is as follows:
我尝试的代码如下:
var dataO = new Object();
dataO.numberId = 1;
dataO.companyId = 531;
$.ajax({
type: "POST",
url: "TelephoneNumbers.aspx/DeleteNumber",
data: "{numberId:1,companyId:531}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('In Ajax');
}
});
I would like to pass the object dataO as the ajax data, how can I do it?
我想将对象dataO作为ajax数据传递,我该怎么做?
回答by redsquare
I will leave my original answer in place but the below is how you need to approach it. (Forgive me but it is a long time since I have used regular asp.net / web services with jquery:)
我将保留我的原始答案,但以下是您需要如何处理它。(请原谅我,但我已经很长时间没有在 jquery 中使用常规的 asp.net/web 服务了:)
You need to use the following js lib json2library, you can then use the stringify method to ensure your json is in the correct format for the service.
您需要使用以下 js lib json2库,然后您可以使用 stringify 方法来确保您的 json 为服务的正确格式。
var data0 = {numberId: "1", companyId : "531"};
var json = JSON2.stringify(data0 );
$.ajax({
type: "POST",
url: "TelephoneNumbers.aspx/DeleteNumber",
data: json,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('In Ajax');
}
});
UPDATE: Same issue / answer here
更新:同样的问题/答案在这里
回答by Rex
All arrays passed to php must be object literals. Here's an example from JS/jQuery:
传递给 php 的所有数组都必须是对象字面量。这是来自 JS/jQuery 的示例:
var myarray = {}; //must be declared as an object literal first
myarray[fld1] = val; // then you can add elements and values
myarray[fld2] = val;
myarray[fld3] = Array(); // array assigned to an element must also be declared as object literal
etc...`
等等...`
It can now be sent via Ajax in the data: parameter as follows:
现在可以在 data: 参数中通过 Ajax 发送,如下所示:
data: { new_name: myarray },
php picks this up and reads it as a normal array without any decoding necessary. Here's an example:
php 接收它并将其作为普通数组读取,无需任何解码。下面是一个例子:
$array = $_POST['new_name']; // myarray became new_name (see above)
$fld1 = array['fld1'];
$fld2 = array['fld2'];
etc...
However, when you return an array to jQuery via Ajax it must first be encoded using json. Here's an example in php:
但是,当您通过 Ajax 将数组返回给 jQuery 时,它必须首先使用 json 进行编码。这是php中的一个例子:
$return_array = json_encode($return_aray));
print_r($return_array);
And the output from that looks something like this:
其输出如下所示:
{"fname":"James","lname":"Feducia","vip":"true","owner":"false","cell_phone":"(801) 666-0909","email":"[email protected]", "contact_pk":"","travel_agent":""}
{again we see the object literal encoding tags} now this can be read by JS/jQuery as an array without any further action inside JS/JQuery... Here's an example in jquery ajax:
{再次我们看到对象文字编码标签}现在这可以被 JS/jQuery 作为数组读取,而无需在 JS/JQuery 中进行任何进一步的操作......这是 jquery ajax 中的一个示例:
success: function(result) {
console.log(result);
alert( "Return Values: " + result['fname'] + " " + result['lname'] );
}
回答by Villapalos
Is not necessary to pass the data as JSON string, you can pass the object directly, without defining contentType
or dataType
, like this:
没有必要将数据作为 JSON 字符串传递,您可以直接传递对象,无需定义contentType
or dataType
,如下所示:
$.ajax({
type: "POST",
url: "TelephoneNumbers.aspx/DeleteNumber",
data: data0,
success: function(data)
{
alert('Done');
}
});
回答by redsquare
Just pass the object as is. Note you can create the object as follows
只需按原样传递对象。请注意,您可以按如下方式创建对象
var data0 = {numberId: "1", companyId : "531"};
$.ajax({
type: "POST",
url: "TelephoneNumbers.aspx/DeleteNumber",
data: dataO,
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('In Ajax');
}
});
UPDATE seems an odd issue with the serializer, maybe it is expecting a string, out of interest can you try the following.
UPDATE 似乎是序列化程序的一个奇怪问题,也许它需要一个字符串,出于兴趣,您可以尝试以下操作。
data: "{'numberId':'1', 'companyId ':'531'}",
数据:“{'numberId':'1','companyId':'531'}”,
回答by moff
You may pass an object to the data
option in $.ajax
. jQuery will send this as regular post data, just like a normal HTML form.
您可以将对象传递给 中的data
选项$.ajax
。jQuery 会将其作为常规的 post 数据发送,就像普通的 HTML 表单一样。
$.ajax({
type: "POST",
url: "TelephoneNumbers.aspx/DeleteNumber",
data: dataO, // same as using {numberId: 1, companyId: 531}
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
alert('In Ajax');
}
});
回答by Delorme Grant
[object Object]
This means somewhere the object is being converted to a string.
[object Object]
这意味着对象正在某处转换为字符串。
Converted to a string:
转换为字符串:
//Copy and paste in the browser console to see result
var product = {'name':'test'};
JSON.stringify(product + '');
Not converted to a string:
未转换为字符串:
//Copy and paste in the browser console to see result
var product = {'name':'test'};
JSON.stringify(product);