Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
619 views
in Technique[技术] by (71.8m points)

怎样拼接post数据比较舒服?

//提交保存
$('.j-submit').on('click',function () {

    var tab = $('.am-active').attr('id');
    
 var prize_name = $("#prize-panel-" + tab + " input[name='prize_name']").val();   //奖品标题名字
 var prize_imgurl = $("#prize-panel-" + tab + " input[name='prize_imgurl']").val(); //奖品图片地址
 var prize_rank = tab;                                                             //奖品所在圆盘格子的位置
 var prize_percent = $("#prize-panel-" + tab + " input[name='prize_percent']").val(); //奖品获奖概率
 var prize_goods_id = $("#prize-panel-" + tab + " input[name='prize_goods_id']").val(); //奖品商品id
 
 var data = 'prize_name='+prize_name+'&prize_imgurl='+prize_imgurl+'&prize_rank='+prize_rank;
    data += '&prize_percent='+prize_percent+'&prize_goods_id='+prize_goods_id;
    $.ajax({
        //请求方式
 type : "POST",
        //请求的媒体类型
 contentType: "application/x-www-form-urlencoded",
        //请求地址
 url : "http://127.0.0.1/admin/list/",
        //数据,json字符串
 data : data,
        //请求成功
 success : function(result) {
            console.log(result);
        },
        //请求失败,包含具体的错误信息
 error : function(e){
            console.log(e.status);
            console.log(e.responseText);
        }
    });
});

怎样能方便拼接post字符串?这种太恶心了。当前页没有form表单这个标签。


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Answer

0 votes
by (71.8m points)

你只要传data对象就行了,jquery会根据contentType帮你处理成表单的


与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome to OStack Knowledge Sharing Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...