ly-radiata

js 提交表单添加csrf

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();
}

  

发表于 2017-02-15 10:30  如何静  阅读(5153)  评论(0编辑  收藏  举报
 

分类:

技术点:

相关文章:

  • 2021-10-19
  • 2021-10-19
  • 2021-06-04
  • 2021-09-13
  • 2021-10-13
  • 2021-11-27
  • 2021-11-27
猜你喜欢
  • 2021-11-18
  • 2021-11-24
  • 2021-10-19
  • 2021-10-19
  • 2021-10-19
  • 2021-11-29
  • 2021-10-19
相关资源
相似解决方案