How do I submit an name[] array with Ajax?

Source: Internet
Author: User

Today's development comes across a problem: How does jquery's Ajax commit an [name][] array? It is well known that the name plus [] can directly transfer the array to the server, but Ajax does not work, directly using name[] will directly error.

Example:

        $.ajax ({            URL: "${ctx}/addcart",            type: "Post",            data:{"name[]": $ (". pid"). Val ()},            DataType: "Text",            traditional:true,            success:function (data) {                if (data! = "0") {                    $ (". Cartlist span"). Text (Number ($ ("). Cartlist span "). Text ()) +number (data));                    Showbo.Msg.alert ("added to Cart");                else{                    window.location.href= "${ctx}/login";}}        )

The great God suggests that you can serialize a form to commit

        $.ajax ({            URL: "${ctx}/addcart",
Cache:true, type: "Post", data:$ (' #yourformid '). Serialize (), dataType: "Text", traditional:true, success:function (data) { if (data! = "0") { $ (". Cartlist span"). Text (Number ($ (". Cartlist span"). Text ()) + Number (data)); Showbo.Msg.alert ("added to Cart"); else{ window.location.href= "${ctx}/login";}} )

And then the egg, still reported 400

My way of handling:

        $.each ($ (". pid"). Serializearray (), Function (i, field) {            Pid.push (field.value);        });        $.ajax ({            URL: "${ctx}/addcart",            type: "Post",            data:{pid:pid},            dataType: "Text",            traditional: True,            success:function (data) {                if (data! = "0") {                    $ (". Cartlist span"). Text (Number ($ (". Cartlist span"). Text ()) +number (data));                    Showbo.Msg.alert ("added to Cart");                else{                    window.location.href= "${ctx}/login";}}        )    }

Actually very fascinated, do not know why add the brackets on the hehe

How do I submit an name[] array with Ajax?

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.