The HTTP status code "0" is not valid. 报错

环境

本机环境: win 7
homestead:lc-homestead-8.2.1-2019112300()
Laravel version:7.13.0

问题描述

《L05 Laravel 教程 - 电商实战 》(8.6. 用户界面 - 申请退款) 我想测试将错误提示给用户。就将 if (!$order->paid_at) 改为 if ($order->paid_at),使程序进入到错误提示。

// 判断订单是否已付款
if ($order->paid_at) {
    throw new InvalidRequestException('该订单未支付,不可退款');
}

我提交退款申请提示 The HTTP status code "0" is not valid.

结果截图

The HTTP status code "0" is not valid. 报错

代码

app/Http/Controllers/OrdersController.php

namespace App\Http\Controllers;

use Illuminate\Http\Request;
use App\Http\Requests\OrderRequest;
use App\Models\ProductSku;
use App\Models\UserAddress;
use App\Models\Order;
use Carbon\Carbon;
use App\Services\OrderService;
use App\Exceptions\InvalidRequestException;
use App\Http\Requests\SendReviewRequest;
use App\Events\OrderReviewed;
use App\Http\Requests\ApplyRefundRequest;

class OrdersController extends Controller
{
    .
    .
    .
    public function applyRefund(Order $order, ApplyRefundRequest $request)
    {
        // 校验订单是否属于当前用户
        $this->authorize('own', $order);
        // 判断订单是否已付款
        if ($order->paid_at) {
            throw new InvalidRequestException('该订单未支付,不可退款');
        }
        // 判断订单退款状态是否正确
        // if (!($order->refund_status !== Order::REFUND_STATUS_PENDING)) {
        //     throw new InvalidRequestException('该订单已经申请过退款,请勿重复申请');
        // }
        // // 将用户输入的退款理由放入订单的 extra 字段中
        // $extra = $order->extra ?:[];
        // $extra['refund_reason'] = $request->input('reason');
        // // 将订单退款状态改为已申请退款
        // $order->update([
        //     'refund_status' => Order::REFUND_STATUS_APPLIED,
        //     'extra'         => $extra,
        // ]);

        return $order;

    }
}

resources/views/orders/show.blade.php

   .
   .
   .
    // 退款按钮点击事件
        $('#btn-apply-refund').click(function () {
            swal({
                text: '请输入退款理由',
                content: 'input',
            }).then(function (input) {
                // 当用户点击 swal 弹出框上的按钮时触发这个函数
                if (!input) {
                    swal('退款理由不能为空', '', 'error');
                    return;
                }
                // 请求退款接口
                axios.post('{{ route('orders.apply_refund', [$order->id]) }}', {reason: input})
                .then(function(){
                    swal('申请退款成功', '', 'success').then(function () {
                        // 用户点击弹窗上按钮时重新加载页面
                        location.reload();
                    });

                },function(error) {
                    if (error.response && error.response.status === 401) {
                        swal('请先登录', '', 'error');
                    } else if (error.response && (error.response.data.msg || error.response.data.message)) {
                        // 其他有 msg 或者 message 字段的情况,将 msg 提示给用户
                        swal(error.response.data.msg ? error.response.data.msg : error.response.data.message, '', 'error');
                    }  else {
                        // 其他情况应该是系统挂了
                        swal('系统错误', '', 'error');
                    }
                });

            });
        });

App/Exceptions/InvalidRequestException.php

<?php

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Request;

class InvalidRequestException extends Exception
{
    public function __contruct(string $message = "", int $code = 400)
    {
        parent::__contruct($message, $code);
    }

    public function render(Request $request)
    {
        if ($request->expectsJson()) {
            // json() 方法第二个参数就是 Http 返回码
            return response()->json(['msg' => $this->message], $this->code);
        }

        return view('pages.error', ['msg' => $this->message]);
    }
}

尝试

我觉得问题出在App/Exceptions/InvalidRequestException.php。修改InvalidRequestException.php代码,看程序跑到哪一步。修改如下:

namespace App\Exceptions;

use Exception;
use Illuminate\Http\Request;

class InvalidRequestException extends Exception
{
    public function __contruct(string $message = "", int $code = 400)
    {
        parent::__contruct($message, $code);
    }

    public function render(Request $request)
    {
     // 修改部分
        if ($request->expectsJson()) {
            // json() 方法第二个参数就是 Http 返回码
            return response()->json(['msg' => '11111'], 400);
            // return response()->json(['msg' => $this->message], $this->code);
        }
        return response()->json(['msg' => '22222'], 400);
        // return view('pages.error', ['msg' => $this->message]);
    // 修改部分END
    }
}

结果是通过了if ($request->expectsJson())判断。

The HTTP status code "0" is not valid. 报错

接着修改InvalidRequestException.php代码,查看code的值

     if ($request->expectsJson()) {
            // json() 方法第二个参数就是 Http 返回码
            return response()->json(['msg' => "$this->code"], 400);
            // return response()->json(['msg' => $this->message], $this->code);
     }

结果输出0
The HTTP status code "0" is not valid. 报错
根据上面测试,我了解到了code0。这里想不明白code怎么会变成了0,初始化应该是400才对的。

class InvalidRequestException extends Exception
{
    public function __contruct(string $message = "", int $code = 400)
    {
        parent::__contruct($message, $code);
    }
    .
    .
    .

调用的时候也没有传0过去。

        // 判断订单是否已付款
        if ($order->paid_at) {
            throw new InvalidRequestException('该订单未支付不可退款');
        }

我设下code666

       // 判断订单是否已付款
        if ($order->paid_at) {
            throw new InvalidRequestException('该订单未支付不可退款'666);
        }

结果显示666

The HTTP status code "0" is not valid. 报错

走到这里。了解到了code0导致报错The HTTP status code "0" is not valid.。至于code怎么变为0,我不知道怎么查了。求高手指条路

《L01 基础入门》
我们将带你从零开发一个项目并部署到线上,本课程教授 Web 开发中专业、实用的技能,如 Git 工作流、Laravel Mix 前端工作流等。
《L03 构架 API 服务器》
你将学到如 RESTFul 设计风格、PostMan 的使用、OAuth 流程,JWT 概念及使用 和 API 开发相关的进阶知识。
leo
最佳答案

__contruct -> __construct

这就是我为什么极力推荐新手使用 PHPStorm 的理由,可以帮你避免绝大多数低级的、解决起来也没有任何技术成长的而又不容易发现的 bug。

3年前 评论
L学习不停 3年前
讨论数量: 5
leo

__contruct -> __construct

这就是我为什么极力推荐新手使用 PHPStorm 的理由,可以帮你避免绝大多数低级的、解决起来也没有任何技术成长的而又不容易发现的 bug。

3年前 评论
L学习不停 3年前

@leo 好的谢谢

3年前 评论
Phantom 1年前
zerocoder (作者) (楼主) 1年前

讨论应以学习和精进为目的。请勿发布不友善或者负能量的内容,与人为善,比聪明更重要!