python-flask之ajax从前端传递数据到后台

一、get方法

   $.get('/changecart_mount',
         {'mount':mount,'cartid':cartid,'totalprice':totalprice},
          function (data,status) {
                   console.log(data.code,status);
                })
@cart_blurprint.route('/changecart_mount',methods=["GET","POST"])
def changecart_mount():
    cartid = request.args.get("cartid")
    mount = request.args.get("mount")
    totalprice = request.args.get("totalprice")
    print("cartid:",cartid)
    print("mount:",mount)
    print("totalprice:",totalprice)
    return jsonify({"code":200})

注:1.传送数组时 request.args.getlist(“XXX”)
2.数组的新建:var data = [ ]
数组的填入 data.push( )
数组后台获取getlist(“data[ ]”)
要加【】否则可能获得的是空数组

           $("a.checkorder").click(function () {
                var cartids = []
                var chks = $("input[name='select']:checked")
                chks.each(function(i){

                    var cartid = $(this).parents("ul").children(".col03").children(".cartid").text()
                    cartids.push(cartid)

				})
                console.log(cartids)


                $.get('/checkorder',{'cartids':cartids},function (data,status) {
                   console.log(data.code,status);
                })
            })
@cart_blurprint.route('/checkorder',methods=["GET","POST"])
def checkorder():
    cartids = request.args.getlist("cartids[]")
    print("cartids:",cartids)
    return jsonify({"code":200})

二、post方法
同get $.post()
后台:request.form.get()

三、ajax方法
ajax表单数据与JSON数据的传送

你可能感兴趣的:(python-flask之ajax从前端传递数据到后台)