Warning: file_get_contents(/data/phpspider/zhask/data//catemap/0/laravel/11.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

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/311.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
Php Laravel ApiException返回HTML响应而不是JSON_Php_Laravel_Laravel 5 - Fatal编程技术网

Php Laravel ApiException返回HTML响应而不是JSON

Php Laravel ApiException返回HTML响应而不是JSON,php,laravel,laravel-5,Php,Laravel,Laravel 5,我试图弄清楚为什么我的ApiException仍然返回一个text/html响应,而不是ApiException呈现方法中表示的json响应。它给了我正确的错误消息,但是它没有将其呈现为json /** * Get the checklist (depending on type - send from Vue model) */ public function fetchChecklist(Request $request) { $id = $request->input('

我试图弄清楚为什么我的ApiException仍然返回一个text/html响应,而不是ApiException呈现方法中表示的json响应。它给了我正确的错误消息,但是它没有将其呈现为json

/**
 * Get the checklist (depending on type - send from Vue model)
 */
public function fetchChecklist(Request $request)
{
    $id = $request->input('projectId');
    $type = $request->input('type');

    if (empty($id)) {
        throw new ApiException('Project was not provided.');
    }

    if (! $project = RoofingProject::find($id)) {
        throw new ApiException('Project not found.');
    }

    if (empty($type)) {
        throw new ApiException('No checklist type was provided.');
    }

    switch ($request->input('type')) {
        case 'permitting':
            $items = $project->permittingChecklist;
            break;

        case 'permit':
            $items = $project->permitReqChecklist;
            break;

        default:
            throw new ApiException('Checklist not found.');
            break;
    }

    return [
        'status' => 'success',
        'message' => '',
        'items' => $items
    ];
}
App\Exceptions\ApiException.php

<?php

namespace App\Exceptions;

class ApiException extends \Exception
{
    public function render($request)
    {
        return response()->json(['status' => 'error', 'error' => $this->message]);
    }
}

在对API的请求中,您可以尝试将以下内容添加到head/curl调用中,以指定数据类型:

"Accept: application/json"

laravel应用程序正在查找请求是否需要json。

我将下面的头设置为json

"x-requested-with": "XMLHttpRequest"

我觉得在您的代码中应该有一个
catch(ApiException$ex){…}
。它甚至根本没有命中render方法,因为如果我在render方法中执行dd(),它不会转储我添加的任何内容。
会抛出新的ApiException('Project not found')
调用
render
方法而不使用
catch
?您是否需要在处理程序类中调用
catch(ApiException$ex){return$ex->render();}
<代码>如果(方法_存在('render',$exception)){return$exception->report();}
确定。。。我不理解这个实现,我现在所能看到的就是你在代码中抛出了一个未捕获的异常,但我不会第三次用同样的东西来评论。抱歉,我查看了它的内部,它目前确实接受application/json,但没有返回它。