码迷,mamicode.com
首页 > Web开发 > 详细

js 提交表单添加csrf

时间:2017-02-15 10:41:28      阅读:1757      评论:0      收藏:0      [点我收藏+]

标签:ecif   hid   spec   sum   attr   string   this   code   default   

function post(path, shipmentMap, method) {
    method = method || "post"; // Set method to post by default if not specified.
    var token = $(‘meta[name="_csrf"]‘).attr(‘content‘);
    var tokenName = $(‘meta[name="_csrf_header"]‘).attr(‘content‘);
    console.log(token);
    // The rest of this code assumes you are not using a library.
    // It can be made less wordy if you use one.
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);


    var hiddenField = document.createElement("input");
    hiddenField.setAttribute("type", "hidden");
    hiddenField.setAttribute("name", "requestMap");
    hiddenField.setAttribute("value", JSON.stringify(shipmentMap));

    form.appendChild(hiddenField);
    
    var csrfField = document.createElement("input");
    csrfField.setAttribute("type", "hidden");
    csrfField.setAttribute("name", "_csrf");
    csrfField.setAttribute("value", token);

    form.appendChild(csrfField);


    document.body.appendChild(form);
    form.submit();
}

  

js 提交表单添加csrf

标签:ecif   hid   spec   sum   attr   string   this   code   default   

原文地址:http://www.cnblogs.com/ly-radiata/p/6400364.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!