简体   繁体   English

如何发送带有$ .ajax-dataType:json的数组?

[英]How can I sent an array with $.ajax - dataType: json?

Im currently trying to sent a javascript array to my .php file handler and save it to my database. 我目前正在尝试将javascript数组发送到我的.php文件处理程序并将其保存到我的数据库中。

The request is successful however it seems my array doesn't get POSTED / saved correctly. 请求成功,但是似乎我的阵列未正确发布/保存。 In my POST request source it just turns up as: round_items%5B%5D=1 在我的POST请求源中,它只是显示为: round_items%5B%5D=1

What am I missing? 我想念什么?

id = 5;
var roundChallenges = new Array("item1", "item2", "etc");

//Save the data
var url = path.php;

var request = $.ajax({
    type: "POST",
    url: url,
    dataType: 'json',
    data: { uid: id, round_items: roundChallenges },

    success: function(data)
    {....

round_items%5B%5D=1 is correct. round_items%5B%5D=1是正确的。 That is what it should be sending. 那就是它应该发送的。 That decodes to round_items[]=1 , which is how you make arrays in query strings. round_items[]=1解码为round_items[]=1 ,这就是在查询字符串中创建数组的方式。

When you pass an object to $.ajax , jQuery converts it to a query string, a standard transport format. 当您将对象传递给$.ajax ,jQuery会将其转换为查询字符串(一种标准的传输格式)。

In PHP, you don't need json_decode or anything. 在PHP中,您不需要json_decode或其他任何东西。 It will parse it into $_POST for you. 它将为您解析为$_POST $_POST['round_items'] will be an array, and $_POST['uid'] will be your id . $_POST['round_items']将是一个数组,而$_POST['uid']将是您的id

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

相关问题 我如何使用JS和AJAX发送数组 - How can i sent an array with JS and AJAX 我如何访问通过 ajax 发送到 php 文件的 POST 请求的 JSON 数据并将它们存储在数组变量中? - how can i acces the JSON data of POST request sent through ajax to a php file and store them in an array variable? 如何处理jQuery.Ajax中的时间响应(数据类型:JSON)? - How I Handle the time response in jQuery.Ajax (DataType: JSON)? 通过JSON对象发送时如何捕获Ajax页面中的数组 - how to capture array in ajax page when sent through JSON object 如何使用 JQUERY/AJAX 显示包含图片和文本数据的多个数据,这些数据作为对象或数组发送? - How can I show multiple data containing picture and text data with JQUERY/AJAX, which are sent as object or array? jQuery,AJAX:如何用json返回值填充数组? - JQuery, AJAX: How can I populate an array with a json return? 将ajax与数据类型json一起使用 - Using ajax with datatype json 将Array转换为ArrayBuffer时如何获取dataType? - How can i get the dataType when Array is castet into an ArrayBuffer? 如何在javascript中使用自定义“类”(函数)作为数组数据类型? - How can I use custom “class”(function) as array datatype in javascript? 如何在Typescript中为对象数组声明数据类型? - How can I declare a datatype for an array of objects in Typescript?
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM