Home >Backend Development >PHP Tutorial >The front end uses ajax to send an empty array to the back end, but the back end cannot get the empty array!
The thing is like this, I use ajax to interact with the backend and need to send a bunch of data to the backend. I encapsulated the data and it is in the following format! This is send_data in my ajax function below.
The data is an array of objects. The objects contained in the array have two keys, namely role_code and user_list. Among them, user_list is an array by default and can be empty.
So I went to the browser to check the request information and found that I didn’t seem to have sent a few empty arrays.
I am very confused, can’t jqeury’s ajax function pass an empty array? Attached is my ajax code, in which send_data is the data I typed out at the beginning.
<code>$.ajax({ url:"<?php echo site_url('AnnouncementAgent/UserDepartmentConfig/editDepartmentRoleInfo');?>"+"/"+$(this).parents(".modal-content").attr("id"), type:"POST", data:{send_data}, dataType:"json", success:function (data) { if (data.status==1) { alert("成功!"); location.reload(); }else{ alert("失败!"); } } })</code>
The thing is like this, I use ajax to interact with the backend and need to send a bunch of data to the backend. I encapsulated the data and it is in the following format! This is send_data in my ajax function below.
The data is an array of objects. The objects contained in the array have two keys, namely role_code and user_list. Among them, user_list is an array by default and can be empty.
So I went to the browser to check the request information and found that I didn’t seem to have sent a few empty arrays.
I am very confused, can’t jqeury’s ajax function pass an empty array? Attached is my ajax code, in which send_data is the data I typed out at the beginning.
<code>$.ajax({ url:"<?php echo site_url('AnnouncementAgent/UserDepartmentConfig/editDepartmentRoleInfo');?>"+"/"+$(this).parents(".modal-content").attr("id"), type:"POST", data:{send_data}, dataType:"json", success:function (data) { if (data.status==1) { alert("成功!"); location.reload(); }else{ alert("失败!"); } } })</code>
Strictly control the format:
data:JSON.stringify(send_data)
By the way, after the front-end and back-end agree on the format, and the front-end converts it to json encoding, an empty array can be passed (of course, it cannot be called an array after encoding).
Backend (php) json_decode() will do.
One last thing to say, it feels a bit ugly to mix PHP code into the front-end code. . .
name value plus
<code>data:{user_list:send_data},</code>
<code>$.ajax({ type: "POST", url: '', dataType:'json', data: { 参数A: 参数A的值, 参数B: 参数B的值 }, success:function(result){ if (result.success) { }else{ } } });</code>