使用 jQuery 通过 AJAX 将 JSON 发送到 PHP
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/3667762/
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
Sending JSON via AJAX to PHP using jQuery
提问by Odyss3us
I am trying to send JSON to a PHP file using jQuery AJAX, basically what I am trying to do is get the values and id's of a bunch of child elements and then assign them to a JSON object and then send that object via ajax to the PHP file which would then process it and enter it into a database.
我正在尝试使用 jQuery AJAX 将 JSON 发送到 PHP 文件,基本上我想要做的是获取一堆子元素的值和 ID,然后将它们分配给一个 JSON 对象,然后通过 ajax 将该对象发送到PHP 文件,然后将处理它并将其输入到数据库中。
Here is my code,
这是我的代码,
Javascript/jQuery:
Javascript/jQuery:
function test(){
var selects = $('#systems_wrapper').find('.dropDowns');
var newArray = new Array();
selects.each(function(){
var id = $(this).attr('id');
var val = $(this).val();
var o = { 'id': id, 'value': val };
newArray.push(o);
});
$.ajax({
type: "POST",
url: "qwer.php",
dataType: 'json',
data: { json: newArray }
});
}
PHP:
PHP:
<?php
$json = $_POST['json'];
$person = json_decode($json);
$file = fopen('test.txt','w+');
fwrite($file, $person);
fclose($file);
echo 'success?';
?>
It creates the file, but it is completely blank, any idea what it could be?
它创建了文件,但它完全是空白的,知道它可能是什么吗?
Thanx in advance!
提前谢谢!
回答by mattbasta
You could try using the JSON.stringify()
method to convert your array into JSON automagically. Just pass the output from this.
您可以尝试使用该JSON.stringify()
方法将数组自动转换为 JSON。只需通过此输出。
data: { json: JSON.stringify(newArray) }
Hope this helps
希望这可以帮助
回答by Guilherme
Don't use an array.
use a simple string like this:
不要使用数组。
使用一个像这样的简单字符串:
var o = '[';
selects.each(function(){
var id = $(this).attr('id');
var val = $(this).val();
o += '{ "id": "'+id+'", "value": "'+val+'" },';
});
o = o.substring(0,o.length-1);
o += ']';
and in the ajax just send the string 'o'
在 ajax 中只发送字符串 'o'
data: { json: newArray }
in the php file just make a json_decode($json, true);
it will return an array of array that you can access by a foreach
if you want to see the array, use var_dump($person);
在 php 文件中只做一个 json_decode($json, true);
它将返回一个可以通过 foreach 访问的数组数组,
如果您想查看该数组,请使用 var_dump($person);
回答by Marthin
You should set a contentType on your ajax POST. I would use contentType: "application/json";
您应该在 ajax POST 上设置 contentType。我会使用 contentType: "application/json";
回答by Z. Zlatev
You should use json_encode()
not json_decode()
! This way you will get the json string and be able to write it.
你应该用json_encode()
不json_decode()
!通过这种方式,您将获得 json 字符串并能够编写它。
回答by phx_zs
No need to use json_decode if you're saving it to a text file. jQuery is encoding your array in JSON format, PHP should then just write that format right to the text file. When you want to open that file and access the data in a usable way, read its contents into a variable and THEN run json_decode() on it.
如果要将其保存到文本文件,则无需使用 json_decode。jQuery 以 JSON 格式对您的数组进行编码,然后 PHP 应该将该格式直接写入文本文件。当您想打开该文件并以可用方式访问数据时,请将其内容读入一个变量,然后在其上运行 json_decode()。