使用Jquery Ajax将对象作为数据发布

时间:2022-12-05 17:07:11

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数据传递,我该怎么办?

6 个解决方案

#1


30  

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:)

我将保留原来的答案,但以下是您需要接近它的方法。 (原谅我,但是很长一段时间以来我使用常规的asp.net / web服务和jquery :)

You need to use the following js lib json2 library, 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

更新:同样的问题/答案在这里

#2


7  

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:

它现在可以通过Ajax在data:参数中发送,如下所示:

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":"jp@gmail.com", "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'] );
}

#3


3  

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'}”,

#4


1  

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或dataType,如下所示:

$.ajax({
    type: "POST",
    url: "TelephoneNumbers.aspx/DeleteNumber",
    data: data0,

    success: function(data)
    {
        alert('Done');
    }
});

#5


0  

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.

您可以将对象传递给$ .ajax中的数据选项。 jQuery会将其作为常规帖子数据发送,就像普通的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');
    }
});

#6


0  

[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);

#1


30  

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:)

我将保留原来的答案,但以下是您需要接近它的方法。 (原谅我,但是很长一段时间以来我使用常规的asp.net / web服务和jquery :)

You need to use the following js lib json2 library, 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

更新:同样的问题/答案在这里

#2


7  

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:

它现在可以通过Ajax在data:参数中发送,如下所示:

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":"jp@gmail.com", "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'] );
}

#3


3  

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'}”,

#4


1  

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或dataType,如下所示:

$.ajax({
    type: "POST",
    url: "TelephoneNumbers.aspx/DeleteNumber",
    data: data0,

    success: function(data)
    {
        alert('Done');
    }
});

#5


0  

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.

您可以将对象传递给$ .ajax中的数据选项。 jQuery会将其作为常规帖子数据发送,就像普通的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');
    }
});

#6


0  

[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);