简体   繁体   English

Laravel 419 使用 Ajax 和 formData 时出错,

[英]Laravel 419 Error using Ajax and formData,

My form data doesn't send any thing i don't know why i tried to debug line by line and i found the problem in data: formData variabe, can someone help me:我的表单数据没有发送任何东西我不知道为什么我试图逐行调试,我在data: formData变量,有人可以帮助我:

also i can't stop redirection using return false我也无法使用 return false 停止重定向

$(function(){
// When Form #xhr is Submited
$('#xhr').submit(function(){
    var selectedFile = null;
    var data = new FormData();

    //if(selectedFile !== null)
    data.append('image', selectedFile, selectedFile.name)

    $($(form).serializeArray()).each(function(i, field){ 
        data.append(field.name, field.value);
    });


    $.ajax({
        url: $(form).attr('action'),
        data: data,
        dataType: 'JSON',
        cache: false,
        contentType: false, 
        processData: false,
        method: 'POST',
        success: function(data){
            alert(data.success);
        }
    });
    return false;
});
    }); 

use serialize for collecting data使用序列化收集数据

var formData = $('form').serializeArray(),


$.ajax({
    url: $("#myform").attr('action'),
    type: "POST",
    data: formData,
    contentType: "application/json",
    dataType: "json",
    success: function(){
        alert("yes");
    }
});

for token add this in your html对于令牌将其添加到您的 html

    <meta name="csrf-token" content="{{ csrf_token() }}">

then before sending ajax request set this然后在发送 ajax 请求之前设置这个

 $.ajaxSetup({
        headers: {
            'X-CSRF-TOKEN': $('meta[name="csrf-token"]').attr('content')
        }
    });

$.ajax({
    url: $("#myform").attr('action'),
    type: "POST",
    data: formData,
    contentType: "application/json",
    dataType: "json",
    success: function(){
        alert("yes");
    }
});

example:例子:

file:index.blade.php文件:index.blade.php

<!doctype html>
<html lang="en">
<head>
     <meta charset="UTF-8">
     <meta name="csrf-token" content="{{ csrf_token() }}">
     <title>Document</title>
</head>
<body>
    <form action="" id="myform">
        <input type="text" name="first" id="first">
        <input type="text" name="second" id="second">
        <input type="text" name="third" id="third">
        <button type="button" id="mysubmitbutton">submit</button>
    </form>
    <script>
       $("body").on("click", "#mysubmitbutton", function () {
       $.ajaxSetup({
           headers: {
               'X-CSRF-TOKEN': $('meta[name="csrf- token"]').attr('content')          }
        });


       $.ajax({
           type: "POST",
           url: "{{ route('myRouteName') }}",
           data: {
                    'first': $("#first").val(),
                    'second': $("#second").val(),
                    'third': $("#third").val()
                 },
           success: function (msg) {
               alert('wooow');
           }
        });
    });
     </script>
     </body>
 </html>

in this simple example i set csrf token in header then when user click on button i collect data and send ajax request在这个简单的示例中,我在 header 中设置 csrf 令牌,然后当用户单击按钮时,我收集数据并发送 ajax 请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM