如何在js中添加csrf-token到form

时间:2018-03-08 19:57:49

标签: javascript laravel

这是我试图尝试将代码添加到javascript表单的代码。 但它返回“Page Expired”

            var form=document.createElement("form");
            form.setAttribute("method","POST");
            form.setAttribute("enctype","multipart/form-data");
            form.setAttribute("name","MyForm");
            form.setAttribute("action","AddQuestionImage");
            form.setAttribute("onsubmit","return ValidateImageExist()");
            var Input_File=document.createElement("input");
            Input_File.setAttribute("type","file");
            Input_File.setAttribute("name","question_image");

            var Token=document.createElement("input");
            Token.setAttribute("type","hidden");
            Token.setAttribute("name","_token");
            Token.setAttribute("value","{{csrf_token()}}");

            var Submit=document.createElement("input");
            Submit.setAttribute("type","submit");
            Submit.setAttribute("value","Add Image To Question");
            Submit.setAttribute("class","btn btn-warning");

            form.appendChild(Token);
            form.appendChild(Input_File);
            form.appendChild(Submit);
            Div_Control.appendChild(form);

1 个答案:

答案 0 :(得分:0)

尝试一下:

csrf.setAttribute(“ name”,“ _ token”);