如何使用 JavaScript 设置 Content-Type 标头
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/2058797/
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
How to set the Content-Type header using JavaScript
提问by Bob K
How can you set the Content-Type header to "application/x-www-form-urlencoded; charset=UTF-8" using JavaScript?
如何使用 JavaScript 将 Content-Type 标头设置为“application/x-www-form-urlencoded; charset=UTF-8”?
I need to do this so I can view a form with french characters without generating errors.
我需要这样做,以便我可以查看带有法语字符的表单而不会产生错误。
Thanks
谢谢
回答by Parrots
Headers are set by the server delivering the content-type as part of the HTTP message. By the time it's in the browser and running the javascript it's too late. Do you have access to the server-side code? Why not set the content type to utf-8 in there? You can also do it as part of the meta tag in the head.
标头由服务器设置,作为 HTTP 消息的一部分提供内容类型。当它出现在浏览器中并运行 javascript 时,为时已晚。您可以访问服务器端代码吗?为什么不在那里将内容类型设置为 utf-8 呢?您也可以将其作为头部元标记的一部分。
回答by Jacob Relkin
You can add a metatag into the headof the page, or send the header server-side.
您可以在页面的 中添加meta标签head,或在服务器端发送标头。
example,
例子,
<meta http-equiv="Content-type" content="application/x-www-form-urlencoded; charset=UTF-8"/>
<meta http-equiv="Content-type" content="application/x-www-form-urlencoded; charset=UTF-8"/>
on the server-side, say PHP:
在服务器端,说 PHP:
<?php
header( 'Content-type: application/x-www-form-urlencoded; charset=UTF-8' );
?>
that's it!
就是这样!
回答by Aaron Digulla
The content type is set by the server before it sends the HTML to the browser. You can't modify it with JavaScript.
内容类型由服务器在将 HTML 发送到浏览器之前设置。你不能用 JavaScript 修改它。

