js 提交表单添加csrf
Posted 如何静
tags:
篇首语:本文由小常识网(cha138.com)小编为大家整理,主要介绍了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(); }
以上是关于js 提交表单添加csrf的主要内容,如果未能解决你的问题,请参考以下文章
Angular 2:将 csrf 参数添加到 noNgForm