如何使用 JavaScript 设置 Content-Type 标头

发布于 2024-08-17 17:30:22 字数 145 浏览 3 评论 0原文

如何使用 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

温柔一刀 2024-08-24 17:30:22

标头由服务器设置,将内容类型作为 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.

蝶舞 2024-08-24 17:30:22

您可以将 meta 标记添加到页面的 head 中,或将标头发送到服务器端。

例如,

在服务器端 PHP:

<?php
  header( 'Content-type: application/x-www-form-urlencoded; charset=UTF-8' );
?>

就是这样!

You can add a meta tag into the head 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:

<?php
  header( 'Content-type: application/x-www-form-urlencoded; charset=UTF-8' );
?>

that's it!

这样的小城市 2024-08-24 17:30:22

内容类型由服务器在将 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.

浅听莫相离 2024-08-24 17:30:22

我假设你想与服务器进行通信,例如提交一个表单,然后服务器将结果发送给你,其中你需要正确的 Content-type 以允许服务器交流。

如果是这样,那么 XMLHttpRequest.setRequestHeader() 可能帮助。


一个例子

(
  () => {
    const form = document.forms.namedItem("my-query-form")
    form.addEventListener('submit', function (submitEvent) {

      const outputElement = document.getElementById("msg")

      const xhr = new XMLHttpRequest();
      xhr.open("POST", "query", true);
      xhr.setRequestHeader("Content-Type", "application/json"); // <----
      xhr.onload = function (oEvent) {
        if (xhr.status === 200) {
          outputElement.innerHTML = `<p style="color:green;">${xhr.responseText}</p>`;
          setTimeout(function () {
            window.location.href = "/home";
          }, 1000);

        } else {
          outputElement.innerHTML = `<p style="color:red;">Error ${xhr.status}: ${xhr.responseText}</p>`
        }
      };

      const htmlFormControlsCollection = submitEvent.target.elements
      const jsonData = JSON.stringify(
        {
          "username": htmlFormControlsCollection["username"].value,
        });
      xhr.send(jsonData);
      submitEvent.preventDefault();
    }, false);
  }
)()

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

(
  () => {
    const form = document.forms.namedItem("my-query-form")
    form.addEventListener('submit', function (submitEvent) {

      const outputElement = document.getElementById("msg")

      const xhr = new XMLHttpRequest();
      xhr.open("POST", "query", true);
      xhr.setRequestHeader("Content-Type", "application/json"); // <----
      xhr.onload = function (oEvent) {
        if (xhr.status === 200) {
          outputElement.innerHTML = `<p style="color:green;">${xhr.responseText}</p>`;
          setTimeout(function () {
            window.location.href = "/home";
          }, 1000);

        } else {
          outputElement.innerHTML = `<p style="color:red;">Error ${xhr.status}: ${xhr.responseText}</p>`
        }
      };

      const htmlFormControlsCollection = submitEvent.target.elements
      const jsonData = JSON.stringify(
        {
          "username": htmlFormControlsCollection["username"].value,
        });
      xhr.send(jsonData);
      submitEvent.preventDefault();
    }, false);
  }
)()
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文