提问者:小点点

在ajax请求Laravel后显示flash和错误消息


ajax请求成功后,我想向我的视图发送一条flash消息(例如,编辑后,我想使用$flash=“Your shop has update”将用户重定向到主页)。在控制器中,这很容易,但我不知道在JavaScript中做什么。你们有谁知道怎么弄清楚吗?我在用拉威尔

控制器

   public function postUpdate (Request $request)
    {
            $this->validate($request, [
                'website_name' => 'required',
                'website_url' => 'required',
                'category' => 'required',
                'type' => 'required',
                'sells' => 'required',
                'location' => 'required',
                'description' => 'required',
                'payment' => 'required'
            ]);
            Shop::where('username', '=', Auth::user()->username)->update(['website_name' => Input::get('website_name'),
                'website_url' => Input::get('website_url'), 'type' => Input::get('type'), 'category' => Input::get('category'), 'sells' => Input::get('sells'), 'location' => Input::get('location'),
                'payment' => Input::get('payment'), 'description' => Input::get('description')]);
        return Response::json(['message' => 'Success', 'message_class' => 'alert alert-success fade in']);
    }

AJAX

$(".update-form").submit(function(s){

            s.preventDefault();

            var website_name = $('input[name=website_name]').val();
            var website_url = $('input[name=website_url]').val();
            var type = $('#type option:selected').val();
            var category = $('#category option:selected').val();
            var sells = $('input[name=sells]').val();
            var location = $('input[name=location]').val();
            var payment = $('input[name=payment]').val();
            var description = $("textarea#message").val();

                $.ajax({
                type: "POST",
                url: "advertiser/update",
                data: {
                    _token: token, website_name: website_name, website_url: website_url, type: type, category: category, sells: sells, location: location, payment: payment, description: description

                },

                success: function() {
                   $('.modal-backdrop').remove();
                    $('body').load('advertiser')

                },

                error: function(data) {
                    $('body').load('advertiser')

                }
                      })


        });

超文本标记语言

  <div class="row" id="errors">
            @if (Session::has('message'))
                <div class="{!! Session::get('message_class') !!}">
                    <a href="#" class="close" data-dismiss="alert">&times;</a>
                    <strong>Note!</strong> {!! Session::get('message') !!}
                </div>
            @endif

            @if($errors->has())
                <div class="alert alert-danger fade in">
                    <a href="#" class="close" data-dismiss="alert">&times;</a>

                    <p>the following errors have occured:</p>
                    <ul>
                        @foreach($errors->all() as $error)
                            <li>{{$error}}</li>
                        @endforeach
                    </ul>
                </div>

            @endif

共3个答案

匿名用户

如果使用ajax请求,则无法重定向内部控制器。

但你可以这样做。

像这样在控制器中发送一些参数

$Response   = array(
            'success' => '1',
        );

$Response   = array(
            'success' => '0',
            'error' => 'Your Flash Message'
        );

并返回return$Response

然后,在ajax结果中,您可以像这样重定向用户

if (data.success == 1){
    window.location = 'toyourdesiredpath';
}
else
{
//show your error message in your div or span
}

匿名用户

只需在Laravel controller中设置会话闪存消息和重定向url(如果要重新加载当前页面,请返回null),如下所示:

if ($redirect) {
    session()->flash('success', 'Your Flash Message for redirect');
    return '/redirect-url';
} else {
    session()->flash('success', 'Your Flash Message for reload');
    return null;
}

然后在JavaScript代码中进行重定向或重新加载:

$.post(`/ajax-url`, $(e.target).serialize()
  ).done((redirect) => {
    if (redirect) {
      window.location.href = redirect
    } else {
      window.location.reload()
    }
  }).fail((errors) => {
    this.setState({
      errors: errors.responseJSON.errors
    })
  })

匿名用户

您必须对消息使用重定向,请在代码中尝试以下示例:-

返回重定向::到(用户/登录)-