使用jQuery AJAX将JSON对象传递给PHP

时间:2022-12-05 14:32:09

I have 3 different JSON objects I want to concatenate and send over to a PHP file with jQuery AJAX. I can retrieve the JSON object in the PHP file, but cannot figure out how to loop through the results.

我有3个不同的JSON对象,我想用jQuery AJAX连接并发送到PHP文件。我可以在PHP文件中检索JSON对象,但是我不知道如何对结果进行循环。

Here's what I have so far:

这是我目前所拥有的:

    //my 3 JSON objects:

    var json_obj_1{
        "test1_key":"test1_val",
        "test2_key":"test2_val",
        "test3_key":"test3_val"
    }

    var json_obj_2{
        "test4_key":"test4_val"
    }

    var json_obj_3{
        "json_arr":[
              {
                  "test1":"test2",
                  "test3":"test4"
              }
         ]
    }

   //concat these to send over to the PHP file:
   var my_json_obj = json_obj_1.concat(json_obj_2);

   //I found if I didn't stringify obj_3 they didn't concatenate for some reason
   my_json_obj = my_json_obj.concat(JSON.stringify(json_obj_3));


   //my_json_obj  now looks like this:

   {
        "test1_key":"test1_val",
        "test2_key":"test2_val",
        "test3_key":"test3_val"
   }
   {
        test4_key: test4_val
   }
   {
        "json_obj_3":[
            {"test1":"test2","test3":"test4"}
        ]
   }

Based on this question: Sending JSON to PHP using ajax, I don't stringify the final JSON object.

基于这个问题:使用ajax向PHP发送JSON,我没有对最终的JSON对象进行stringify。

Here's the AJAX call to the PHP file:

下面是对PHP文件的AJAX调用:

$.ajax({
        url: 'my_php_file.php',
        data: {my_json_data: my_json_obj},
        type: 'POST',
        async: false,
        dataType: 'json',
        cache:false,
        success:function(data, textStatus, jqXHR){
            console.log('AJAX SUCCESS');
        }, 
        complete : function(data, textStatus, jqXHR){
            console.log('AJAX COMPLETE');
        }
});

This is what it looks like when retrieved in my PHP file:

这就是在PHP文件中检索时的样子:

echo $_POST['my_json_data'];
//outputs:
{
    "test1_key":"test1_val",
    "test2_key":"test2_val",
    "test3_key":"test3_val"
}
{
     test4_key: test4_val
}
{
    "json_obj_3":[
        {"test1":"test2","test3":"test4"}
    ]
}

Here's where I run in to problems. I want to be able to loop through this in a foreach. From what I have read (php: loop through json array) I have to decode the JSON object. However when I do this and loop through the results I get: " PHP Warning: Invalid argument supplied for foreach() ".

这就是我遇到问题的地方。我希望能够在foreach中循环。从我所读到的(php:通过json数组循环)中,我必须对json对象进行解码。但是,当我这样做并循环遍历结果时,我得到了:“PHP警告:为foreach()提供的无效参数”。

Even if I stringify the JSON object before sending it over to the PHP file I get the same problem. I have been banging my head against a brick wall for hours with this. Any help will be massively appreciated.

即使我在将JSON对象发送到PHP文件之前对其进行了字符串化,我也会遇到同样的问题。我的头撞在砖墙上几个小时了。任何帮助都将得到极大的感谢。

Thanks in advance.

提前谢谢。

Solved:

解决:

I was going wrong when concatenating my JSON objects following this question: Merge two json/javascript arrays in to one array. Instead I just put them together like so:

在连接JSON对象时,我犯了一个错误:将两个JSON /javascript数组合并到一个数组中。相反,我只是把它们放在一起:

my_json_obj = '['+json_obj_1+','+json_obj_2+', '+JSON.stringify(json_obj_3)+']';

I then use json_decode in my PHP file which works a treat. http://json.parser.online.fr/ was invaluable in debugging my dodgy JSON. A great tool for JSON beginners.

然后我在我的PHP文件中使用json_decode,这是一种享受。http://json.parser.online.fr/在调试我那可疑的JSON时是无价的。对于JSON初学者来说,这是一个很好的工具。

2 个解决方案

#1


1  

As you say you need to decode the JSON, but your JSON is not well formed so I think the decode function is failing which causes the foreach error. There needs to be a , between objects, there's missing quotes around test4_val and it needs to be inside a singular object/array

正如您所说,您需要解码JSON,但是您的JSON格式不是很好,所以我认为decode函数的失败导致了foreach错误。在对象之间,test4_val周围需要有一个缺失的引号,并且必须在一个单独的对象/数组中

You can check your JSON is wellformed at:-

您可以在:-查看您的JSON是否格式良好

http://json.parser.online.fr/

http://json.parser.online.fr/

I think you want it to look more like:-

我想你希望它看起来更像:-

[{
"test1_key":"test1_val",
"test2_key":"test2_val",
"test3_key":"test3_val"
},
{
 "test4_key": "test4_val"
},
{
"json_obj_3":[
    {"test1":"test2","test3":"test4"}
]
}]

#2


3  

You should be stringifying the whole object, try the following:

您应该对整个对象进行约束,尝试以下操作:

var my_json_obj = json_obj_1;

$.each(json_obj_2, function(key,value){
    my_json_obj[key]=value;
});
$.each(json_obj_3, function(key,value){
    my_json_obj[key]=value;
});

Ajax request:

Ajax请求:

data: {my_json_data: JSON.stringify(my_json_obj)},    

PHP:

PHP:

print_r(json_decode($_POST['my_json_data']));

#1


1  

As you say you need to decode the JSON, but your JSON is not well formed so I think the decode function is failing which causes the foreach error. There needs to be a , between objects, there's missing quotes around test4_val and it needs to be inside a singular object/array

正如您所说,您需要解码JSON,但是您的JSON格式不是很好,所以我认为decode函数的失败导致了foreach错误。在对象之间,test4_val周围需要有一个缺失的引号,并且必须在一个单独的对象/数组中

You can check your JSON is wellformed at:-

您可以在:-查看您的JSON是否格式良好

http://json.parser.online.fr/

http://json.parser.online.fr/

I think you want it to look more like:-

我想你希望它看起来更像:-

[{
"test1_key":"test1_val",
"test2_key":"test2_val",
"test3_key":"test3_val"
},
{
 "test4_key": "test4_val"
},
{
"json_obj_3":[
    {"test1":"test2","test3":"test4"}
]
}]

#2


3  

You should be stringifying the whole object, try the following:

您应该对整个对象进行约束,尝试以下操作:

var my_json_obj = json_obj_1;

$.each(json_obj_2, function(key,value){
    my_json_obj[key]=value;
});
$.each(json_obj_3, function(key,value){
    my_json_obj[key]=value;
});

Ajax request:

Ajax请求:

data: {my_json_data: JSON.stringify(my_json_obj)},    

PHP:

PHP:

print_r(json_decode($_POST['my_json_data']));