laravel 4 为什么在发送 json 时发送内容类型?
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/19607215/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
laravel 4 why send content-type when sending json?
提问by Darius.V
In the book of laravel I read, and also my co-worker who has experience with laravel said that generating JSON I should in laravel way.
在我读过的 Laravel 书中,我的有 Laravel 经验的同事也说我应该以 Laravel 的方式生成 JSON。
Why do I need to do this:
为什么我需要这样做:
Route::get('markdown/response', function()
{
$data = array('iron', 'man', 'rocks');
return Response::json($data);
});
As I read it sends also content-type header when using this.
当我阅读它时,它还会在使用它时发送内容类型标头。
When I was using codeigniter I used to do simply this:
当我使用 codeigniter 时,我曾经简单地这样做:
echo json_endode($data);
and never ever ever had any problems. Even if it is not set content type. Actually I dont know maybe php sets it automatically, but since I did not have problems, I did not care.
从来没有遇到过任何问题。即使没有设置内容类型。其实我不知道也许php会自动设置它,但因为我没有问题,所以我不在乎。
And when using 'new' technology I really want to know why it is better than good old one.
当使用“新”技术时,我真的很想知道为什么它比好的旧技术更好。
回答by Andreas
The response object returned from Response::json
(and other Response
static methods) are highly modifiable.
从Response::json
(和其他Response
静态方法)返回的响应对象是高度可修改的。
$response = Response::json($data);
$response->header('Content-Type', 'application/json');
return $response;
You can check for more available methods in the Laravel and Symfony code API.
您可以在 Laravel 和 Symfony 代码 API 中查看更多可用方法。
http://laravel.com/api/class-Illuminate.Http.Response.htmlhttp://api.symfony.com/2.1/Symfony/Component/HttpFoundation/Response.html
http://laravel.com/api/class-Illuminate.Http.Response.html http://api.symfony.com/2.1/Symfony/Component/HttpFoundation/Response.html
回答by fideloper
With respect, by not providing a content-type
header, you were doing it "wrong" when coding in CodeIgniter.
顺便说一句,通过不提供content-type
标题,您在 CodeIgniter 中编码时“错误”了。
Most clients (browsers, ajax requests, especially jQuery) can still can guess how to handle the response correctly and so probably "just worked" for you. You were likely always implicitly returning a Content-Type: text/html
with your response, which is a default header in CodeIgniter.
大多数客户端(浏览器、ajax 请求,尤其是 jQuery)仍然可以猜测如何正确处理响应,因此可能对您来说“刚刚工作”。您可能总是Content-Type: text/html
在响应中隐式返回 a ,这是 CodeIgniter 中的默认标头。
You should alwaysreturn a proper content type with your HTTP responses so the consuming client knows how to treat this content. Note that this is a mechanism of HTTP as defined in specification, not specific to any framework or even a language.
您应该始终使用 HTTP 响应返回正确的内容类型,以便消费客户端知道如何处理此内容。请注意,这是规范中定义的 HTTP 机制,并非特定于任何框架甚至语言。
Response::json()
The above code is just a convenience function, where Laravel will automatically set the application/json
header for you, as well as convert an array of data into JSON format. The only effective difference from your CodeIgniter code is the setting of the header, as you've pointed out.
上面的代码只是一个方便的函数,Laravel 会自动application/json
为你设置header,以及将数据数组转换为JSON格式。正如您所指出的,与您的 CodeIgniter 代码的唯一有效区别是标头的设置。
It's worth noting that the Response object extends Symfony's response object, which is very "powerful" - in other words, it's a very good implementation of the HTTP protocol.
值得注意的是,Response 对象扩展了 Symfony 的 response 对象,非常“强大”——换句话说,它是 HTTP 协议的一个很好的实现。
回答by ceejayoz
Just because it worked doesn't mean it wasn't wrong. JSON isn't HTML, so text/html
is an inaccurate Content-Type
for it.
仅仅因为它有效并不意味着它没有错。JSON 不是 HTML,因此text/html
它不准确Content-Type
。
Sending the correct header means libraries like jQuery understand what sort of data they're getting back, and thus are able to handle it on their own without guidance. Browsers may also do things like pretty-printing the JSON data or making it otherwise easier to read.
发送正确的标头意味着像 jQuery 这样的库了解它们返回的数据类型,因此能够在没有指导的情况下自行处理它。浏览器也可能会做一些事情,比如漂亮地打印 JSON 数据或使其更易于阅读。
回答by Cody Covey
Depends what you are trying to do with the route. if you only want to return json data you can just return json_encode($data) and that will work, To actually return a json response for use with something like an ajax request you need the headers set properly or the accepting route just thinks its getting a string. Response::json is for setting the response which sets the headers appropriately.
取决于您要对路线做什么。如果您只想返回 json 数据,您可以只返回 json_encode($data) 并且这将起作用,要实际返回 json 响应以用于类似 ajax 请求的内容,您需要正确设置标头或接受路由认为它正在获取一个字符串。Response::json 用于设置适当设置标头的响应。