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

  


免責聲明!

本站轉載的文章為個人學習借鑒使用,本站對版權不負任何法律責任。如果侵犯了您的隱私權益,請聯系本站郵箱yoyou2525@163.com刪除。



 
粵ICP備18138465號   © 2018-2025 CODEPRJ.COM