Laravel 返回文本/html 而不是 json?

标签 laravel laravel-5 laravel-5.2

我在响应 header 中有这个:

Response headers (165 B)    
    Host    
    "localhost:8000"
    [Learn More]
    Connection  
    "close"
    [Learn More]
    Content-Type    
    "text/html; charset=UTF-8"
    [Learn More]
    Date    
    "Thu, 16 Mar 2017 14:33:53 GMT"
    [Learn More]
    Transfer-Encoding   
    "chunked"

我的 Controller 非常简单:

public function login(Request $request){

      return response()->json([
        'name' => 'Abigail',
        'state' => 'CA'
        ]);
    }

我使用 angular 进行发布。

最佳答案

Laravel 使用 Response 类提供修改头部信息。

在你的 Controller 中尝试这种方式:

$contents = View::make('embedded')->with('foo', $foo);
$response = Response::make($contents, $statusCode);
$response->header('Content-Type', 'text/plain');
return $response;

关于Laravel 返回文本/html 而不是 json?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/42837126/

相关文章:

laravel - 无法使用 SendGrid 中的 2 个可能的身份验证器使用用户名 "apikey"在 SMTP 服务器上进行身份验证

laravel - 没有计划的命令准备好运行 |仅在 $schedule

laravel - 如何将验证规则应用于数组中存在的每个元素?

laravel - 覆盖 Laravel 队列监听器输出 [仅限 Laravel <=5.2]

php - 基于日期的 Laravel 日志文件

php - Laravel 5.1 播种新专栏

Laravel 5 销毁所有用户 session 并强制注销

php - fatal error : Class not found Laravel on routes

php - Laravel 屈服并扩展不工作

php - Laravel mysql 连接问题