【问题标题】:how to get success or failure response from controller to ajax in laravel?如何从控制器获得成功或失败响应到 laravel 中的 ajax?
【发布时间】:2021-02-22 05:09:50
【问题描述】:

我想在成功或失败时显示烤面包机,所以我尝试了这段代码

$.ajax({
            type: "GET",
            dataType: "json",
            url: '{{ route('users.update.visit_clear') }}',
            data: {'visit_clear': visit_clear, 'user_id': userId , 'location': location , 'cs_location': cslocation },
            success: function (data) {
                if (data.message != ""){
                    toastr.success(data.message);
                }
            }
            error: function (data) {
                    toastr.error(data.message1);
            }
        });

在控制器中我有条件

if($var <= 1){
return response()->json(['message' => 'User status updated successfully.' ]);
                }
                else{
                     return response()->json(['message1' => 'Visit Failed Distance is too long' ]);
                }

如果我使用 error:function 它不会响应我

【问题讨论】:

    标签: javascript css laravel styles angular-toastr


    【解决方案1】:

    要在 error ajax 回调中,您必须返回一个 http 错误代码(例如:500、400 等)。
    您可以在此处查看不同的 http 错误代码 https://en.wikipedia.org/wiki/List_of_HTTP_status_codes
    您可以在 else 响应中添加 status code 作为第二个参数
    例如:return response()-&gt;json(['message1' =&gt; 'Visit Failed Distance is too long' ],500);

    【讨论】:

      猜你喜欢
      • 2017-08-06
      • 2021-12-20
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2019-08-13
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多