使用jQuery通过AJAX将JSON发送到PHP

时间:2022-10-17 13:06:05

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!

Thanx提前!

5 个解决方案

#1


18  

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

希望这可以帮助

#2


2  

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);如果你想看到数组,它将返回一个你可以访问的数组数组,使用var_dump($ person);

#3


1  

You should set a contentType on your ajax POST. I would use contentType: "application/json";

您应该在ajax POST上设置contentType。我会使用contentType:“application / json”;

#4


1  

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字符串并能够编写它。

#5


1  

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()。

#1


18  

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

希望这可以帮助

#2


2  

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);如果你想看到数组,它将返回一个你可以访问的数组数组,使用var_dump($ person);

#3


1  

You should set a contentType on your ajax POST. I would use contentType: "application/json";

您应该在ajax POST上设置contentType。我会使用contentType:“application / json”;

#4


1  

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字符串并能够编写它。

#5


1  

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()。