如何使用 JavaScript 设置 Content-Type 标头
如何使用 JavaScript 将 Content-Type 标头设置为“application/x-www-form-urlencoded; charset=UTF-8”?
我需要这样做,以便我可以查看包含法语字符的表单而不会产生错误。
谢谢
How can you set the Content-Type header to "application/x-www-form-urlencoded; charset=UTF-8" using JavaScript?
I need to do this so I can view a form with french characters without generating errors.
Thanks
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
标头由服务器设置,将内容类型作为 HTTP 消息的一部分进行传递。当它进入浏览器并运行 JavaScript 时,已经太晚了。您有权访问服务器端代码吗?为什么不将内容类型设置为 utf-8 呢?您也可以将其作为 head 中元标记的一部分。
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.
您可以将
meta
标记添加到页面的head
中,或将标头发送到服务器端。例如,
在服务器端 PHP:
就是这样!
You can add a
meta
tag into thehead
of the page, or send the header server-side.example,
<meta http-equiv="Content-type" content="application/x-www-form-urlencoded; charset=UTF-8"/>
on the server-side, say PHP:
that's it!
内容类型由服务器在将 HTML 发送到浏览器之前设置。您无法使用 JavaScript 修改它。
The content type is set by the server before it sends the HTML to the browser. You can't modify it with JavaScript.
我假设你想与服务器进行通信,例如提交一个表单,然后服务器将结果发送给你,其中你需要正确的
Content-type
以允许服务器交流。如果是这样,那么 XMLHttpRequest.setRequestHeader() 可能帮助。
一个例子
I assume that you want to communicate with the server, for example, to submit a form, and then the server sends you back the results, in which you need the correct
Content-type
to allow the server to communicate.if so, then XMLHttpRequest.setRequestHeader() may help.
an example