Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/249.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在不刷新页面的情况下使用ajax提交表单请求并在laravel中返回消息?_Javascript_Php_Jquery_Laravel - Fatal编程技术网

Javascript 如何在不刷新页面的情况下使用ajax提交表单请求并在laravel中返回消息?

Javascript 如何在不刷新页面的情况下使用ajax提交表单请求并在laravel中返回消息?,javascript,php,jquery,laravel,Javascript,Php,Jquery,Laravel,我正在尝试提交表单请求并在不刷新页面或重定向的情况下存储数据。并将成功消息发送到 @if (session('message')) <div class="alert alert-success"> {{ session('message') }} </div> @endif html表单: <form method="POST" action="/mail/store" id="contactForm"> @c

我正在尝试提交表单请求并在不刷新页面或重定向的情况下存储数据。并将成功消息发送到

 @if (session('message'))
    <div class="alert alert-success">
       {{ session('message') }}
    </div>
   @endif
html表单:

<form method="POST" action="/mail/store" id="contactForm">
    @csrf
    <div style="display: flex;" class="name-email">
        <div class="form-group col-sm-6 fl_wrap">

            <input type="text" name="name" value="{{ old('name') }}" id="name"
                   class="form-control fl_input" placeholder="Name"
                   required>
        </div>
        <div class="form-group col-sm-6 fl_wrap">

            <input type="text" name="email" value="{{ old('email') }}" id="email"
                   placeholder="Email"
                   class="form-control fl_input" required>
        </div>
    </div>
    <div class="form-group col-sm-12 fl_wrap mt-2">
    <textarea type="text" name="message" rows="7" value="{{ old('message') }}" id="message"
              class="form-control fl_input" placeholder="message"
              required></textarea>
    </div>
    <div class="form-group text-center mt-5">
        <button type="submit" value="save" id="submit" class="btn btn-warning submit"
                style="color: white;background: rgb(59, 199, 246); border-color: rgb(59, 199, 246); width: 140px;">
            send
        </button>
    </div>

</form>

路线

Route::get('/mail','MailsController@create');

Route::post('/mail/store','MailsController@store');

试试这个:

$.post("{{ url('mail/store') }}", {
    name, email, message, '_token': "{{ csrf_token() }}"
}, function(data) {
    $('#postRequestData').html(data.message);
});

告诉我们更多关于ajax调用的详细信息,您是否收到响应?否。我没有收到reponse@tuytoosh如果您在客户端没有响应,请尝试
return response()->json($result)我仍然得到相同的白色页面,响应为{“msg”:“Setting created successfully”}我编辑了我的响应。如果您在客户端有响应,并且您的元素具有
postRequestData
id,那么这应该可以工作。。。
 public function store(Request $request)
    {

        $mail = new Mail();
        $mail->name = $request->name;
        $mail->mail = $request->email;
        $mail->message = $request->message;
        $mail->save();

        $result = array(
            'msg' => 'Setting created successfully' );
        return Response::json($result);
    }

Route::get('/mail','MailsController@create');

Route::post('/mail/store','MailsController@store');

$.post("{{ url('mail/store') }}", {
    name, email, message, '_token': "{{ csrf_token() }}"
}, function(data) {
    $('#postRequestData').html(data.message);
});