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?