Home  >  Article  >  Backend Development  >  The front end uses ajax to send an empty array to the back end, but the back end cannot get the empty array!

The front end uses ajax to send an empty array to the back end, but the back end cannot get the empty array!

WBOY
WBOYOriginal
2016-08-25 10:37:282611browse

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 front end uses ajax to send an empty array to the back end, but the back end cannot get the empty array!

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.

This array has been successfully sent to the backend, but the user_list with a length of 0 cannot be obtained by the backend! Any user_list whose length is not 0 can be obtained by the backend

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.

The front end uses ajax to send an empty array to the back end, but the back end cannot get the empty array!

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>

Please ask God to clarify my doubts!

Reply content:

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 front end uses ajax to send an empty array to the back end, but the back end cannot get the empty array!

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.

This array has been successfully sent to the backend, but the user_list with a length of 0 cannot be obtained by the backend! Any user_list whose length is not 0 can be obtained by the backend

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.

The front end uses ajax to send an empty array to the back end, but the back end cannot get the empty array!

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>

Please ask God to clarify my doubts!

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>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn