如何设置正确的 json 标头?

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/3333196/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-09-03 17:33:15  来源:igfitidea点击:

How do I set the correct json headers?

jsonheadergetjson

提问by Matrym

Is there a way in htaccess to ensure the headers for my json are correct?

htaccess 中有没有办法确保我的 json 的标头正确?

Update: Does anyone see anything wrong with these headers for json?

更新:有没有人发现 json 的这些标头有什么问题?

Date    Mon, 26 Jul 2010 08:31:11 GMT
Server  Apache/2.2.15 (Unix) mod_ssl/2.2.15 OpenSSL/0.9.7a mod_fcgid/2.3.5 Phusion_Passenger/2.2.15 mod_auth_passthrough/2.1 mod_bwlimited/1.4 FrontPage/5.0.2.2635
X-Powered-By    PHP/5.2.13
X-Pingback  http://brettbarros.com/wordpress/xmlrpc.php
Content-Disposition attachment; filename="json_api.json"
Vary    Accept-Encoding
Content-Encoding    gzip
Content-Length  719
Keep-Alive  timeout=5, max=98
Connection  Keep-Alive
Content-Type    application/json; charset=UTF-8

Specifically, it's working with jquery's getJSON in ie8, ffx, chrome, but not ie7 or ie6...

具体来说,它在 ie8、ffx、chrome 中使用 jquery 的 getJSON,但不适用于 ie7 或 ie6...

采纳答案by Roman

You can inspect the headers sent along from the server side using Firebug'sNet tab. It shows all the headers for both the request and the response.

您可以使用Firebug 的Net 选项卡检查从服务器端发送的标头。它显示了请求和响应的所有标头。

回答by Boldewyn

AddType application/json .json

is a simple way to make all your *.jsonfiles being sent with the correct mime type. That, of course, doesn't work, if you create them dynamically in something like a, say, PHP script. In that case, you can add the info inside the script:

是一种使所有*.json文件都以正确的 mime 类型发送的简单方法。当然,如果您在诸如 PHP 脚本之类的东西中动态创建它们,那当然是行不通的。在这种情况下,您可以在脚本中添加信息:

<?php
header('Content-Type: application/json');
// ...

回答by meder omuraliev

Make sure the Content-Type is application/json. You can inspect the http headers with wget and whatnot if you aren't sure what they are.

确保内容类型为application/json. 如果您不确定它们是什么,您可以使用 wget 等检查 http 标头。