【问题标题】:display flash and error message after ajax request Laravelajax 请求 Laravel 后显示 flash 和错误消息
【发布时间】:2016-02-15 00:26:24
【问题描述】:

在成功的 ajax 请求后,我想向我的视图发送一条 flash 消息(例如,在编辑时,我想使用 $flash = "Your shop has been update" 将用户重定向到主页)。在控制器中,这很容易,但我不知道在 JavaScript 中该做什么。你们中有人知道如何解决吗?我正在使用 Laravel

控制器

   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')

                }
                      })


        });

HTML

  <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

【问题讨论】:

  • 收到ajax结果后要重定向页面吗?
  • 不,我已经更改为 Respone::json 我想知道如何在 javascript(jquery) @sulthanAllaudeen 中获取数据
  • @Juam 发布了答案,你想要 Jquery 中的完整脚本吗?

标签: php jquery ajax laravel flash-message


【解决方案1】:

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

return Redirect::to('user/login')->with('message', 'Login Failed');

【讨论】:

  • return Redirect::to('advertiser')->with(['message' => 'Success', 'message_class' => 'alert alert-success fade in']);如果我删除 ajax 它的工作就不能工作...
  • 做一件事以 json 形式返回一些状态,然后在 ajax 成功时您可以显示该成功消息
  • return Response::json or return json_encode 我不知道如何使用 javascript 获取它,你能给我一个例子吗,谢谢 :)
  • 在这里您可以找到更相关的答案:- stackoverflow.com/questions/15624075/…
【解决方案2】:

如果你使用 ajax 请求,你不能重定向内部控制器。

但是你可以这样做。

像这样在Controller中发送一些参数

$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
}

【讨论】:

  • 这不会显示消息,您仍然需要设置会话。
【解决方案3】:

只需在 Laravel 控制器中设置一个会话 flash 消息和一个重定向 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
    })
  })

【讨论】:

  • 对我有用。谢谢
  • 对我不起作用。我正在使用 laravel 8.*。
猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2020-04-28
  • 2021-08-17
  • 1970-01-01
  • 2016-06-14
  • 2014-01-28
  • 2017-12-20
相关资源
最近更新 更多