AJAX将数组数据作为JSON对象发送

时间:2017-07-22 18:34:33

标签: javascript php json ajax

我有一个表单,我想要获取表单的特定元素并将值存储在数组中。

var that = $(this),
url = that.attr('action'),
type = that.attr('method'),

data = {};

var item_name = miniform.elements['itemId'].value;
var quantity = miniform.elements['quantityId'].value;
var amount = miniform.elements['amountId'].value;
var total = amount * quantity;
var cart = ["Item: ",item_name,"Quantity: ",quantity,"Amount: ",amount,"Total: ",total];

我想将此数组转换为JSON对象并通过ajax将其发送到php文件,但它不起作用。这是我的ajax代码:

$.ajax({
    url: url,
    type: type,
    Content-Type:'application/JSON',
    data: JSON.stringify(cart),
    success: function(){
        console.log('Message Sent');
     }
    });

可能是什么问题?

5 个答案:

答案 0 :(得分:0)

您可以使用formdata()发送整个表单。你有没有理由?

无论如何,它不起作用,因为你没有创建一个json对象,而是一个普通的数组。有区别。实际上你可以直接将json放入ajax调用中,而不必将其字符串化。

var cart = {};
cart['Item'] = item_name;
cart['Quantity'] = quantity;
cart['Amount'] = amount;
cart['Total'] = total;

$.ajax({
  url: 'https://www.google.com',
  type: "POST",
  //Datatype causes it to automatically become json and will expect json back
  //return json back from php through json_encode($myArray);       
  dataType: "json",
  data: cart,
  success: function() {
    alert('Message Sent');
  }
});

简而言之:你使用了错误的括号:)

我只是将其更改为更简单的管理对象的方式。您之前的对象的问题是它构建错误。应该是这样的:

var cart = {"Item":item_name,"Quantity": quantity,"Amount":amount,"Total":total};

答案 1 :(得分:0)

你做错了。

请用这种方式

var cart = {"Item ":item_name,"Quantity ":quantity,"Amount ":amount,"Total ":total};

$.ajax({
url: url,
type: type,
Content-Type:'application/JSON',
data: JSON.parse(cart),
success: function(){
    console.log('Message Sent');
 }
});

如果你想发布数组那么你可以用这种方式。

cart = [];
cart[0] = item_name;
cart[1] = quantity;
cart[2] = amount;
cart[3] = total;

$.ajax({
url: url,
type: type,
Content-Type:'application/JSON',
data:{cart:cart},
success: function(){
    console.log('Message Sent');
 }
});

答案 2 :(得分:0)

查看逗号,它应该是{"key":value, "key":value..}而不是{"key":,value, "key":,value..}

var cart = {"Item" : item_name, "Quantity" : quantity, "Amount" : amount, "Total" : total};

答案 3 :(得分:0)

不要手动构建

不是手动构建您的对象或数组,而是容易出错,请执行以下操作:

对象

// Define your empty Object
var cart = {};

// Assign its properties
cart.item_name = miniform.elements['itemId'].value;
cart.quantity = miniform.elements['quantityId'].value;
cart.amount = miniform.elements['amountId'].value;
cart.total = amount * quantity;

阵列

或者如果你想要一个数组:

// Define your empty array
var cart = [];

// Assign its properties
cart['item_name'] = miniform.elements['itemId'].value;
cart['quantity'] = miniform.elements['quantityId'].value;
cart['amount'] = miniform.elements['amountId'].value;
cart['total'] = amount * quantity;

现在您已准备好cart对象或数组,您可以在其上调用JSON.stringify

JSON.stringify(cart);

答案 4 :(得分:0)

我找到了解决问题的方法,并结合了一些您的回答来找到答案。

$('#miniform').on('submit',function(){  
    var that = $(this),
    url = that.attr('action'),
    type = that.attr('method'),

    data = {};
    var cart = {};

    cart.item_name = miniform.elements['itemId'].value;
    cart.quantity = miniform.elements['quantityId'].value;
    cart.amount = miniform.elements['amountId'].value;
    cart.total = cart.amount * cart.quantity;

    var jsonString = JSON.stringify(cart);

   $.ajax({
    url: url,
    type: type,
    data:{data:jsonString},
    success: function(){
        console.log('Email Sent');
     },
     error: function(error){
     throw new Error('Did not work');
      }
    });
 return false;
 });