The front end uses Ajax to send an empty array to the back end, and the backend gets the empty array!

Source: Internet
Author: User

The thing is, I use Ajax and back-end interaction, need to send a bunch of data to the backend, I put the data encapsulated a bit after the format is the following! This is what I send_data in the AJAX function below.

The data is an array of objects, with two keys in the number of groups, Role_code and User_list, respectively. Where User_list is also the default is an array, can be empty.

This array has been successfully sent to the backend, that is, the length inside is 0 user_list back end gets not! The User_list backend, which is not 0 in length, is available

So I went to the browser to view the request information and found that I did not seem to have a few empty arrays sent past.

I was puzzled that the Jqeury Ajax function could not pass an empty array over it? Attached to my Ajax code, where the send_data is the data I started to play.

$.ajax({    url:"
  
   "+"/"+$(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("失败!");    }                       }})

Ask the great God for answers!

Reply content:

The thing is, I use Ajax and back-end interaction, need to send a bunch of data to the backend, I put the data encapsulated a bit after the format is the following! This is what I send_data in the AJAX function below.

The data is an array of objects, with two keys in the number of groups, Role_code and User_list, respectively. Where User_list is also the default is an array, can be empty.

This array has been successfully sent to the backend, that is, the length inside is 0 user_list back end gets not! The User_list backend, which is not 0 in length, is available

So I went to the browser to view the request information and found that I did not seem to have a few empty arrays sent past.

I was puzzled that the Jqeury Ajax function could not pass an empty array over it? Attached to my Ajax code, where the send_data is the data I started to play.

$.ajax({    url:"
  
   "+"/"+$(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("失败!");    }                       }})

Ask the great God for answers!

Strict control format:
Data:JSON.stringify (Send_data)

By the way, the front and back end of a good format, after the first side of the switch to JSON encoding, it is possible to pass an empty array (of course, after the code is not called an array).
Back-end (PHP) Json_decode () on the line.

Finally say a bit more, the front-end code inside the PHP code feel a bit ugly ...

The name value Plus

data:{user_list:send_data},

$.ajax({        type: "POST",        url: '',        dataType:'json',        data: {            参数A: 参数A的值,            参数B: 参数B的值        },        success:function(result){            if (result.success) {                            }else{                            }        }    });
  • Related Article

    Contact Us

    The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

    If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

    A Free Trial That Lets You Build Big!

    Start building with 50+ products and up to 12 months usage for Elastic Compute Service

    • Sales Support

      1 on 1 presale consultation

    • After-Sales Support

      24/7 Technical Support 6 Free Tickets per Quarter Faster Response

    • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.