资源被解释为文档,但在 Chrome 开发者工具中传输时带有 MIME 类型 application/json 警告

发布于 2024-11-27 13:24:33 字数 1694 浏览 1 评论 0原文

我有以下代码片段,它使用 jQuery Form 插件将表单发布到服务器(在 ajax 中)。

  var options = {
    dataType: "json",
    success: function(data) { 
      alert("success");
    } 
  }; 

  $form.ajaxSubmit(options);

形式:

<form enctype="multipart/form-data" id="name_change_form" method="post" action="/my_account/"> 
<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='6c9b552aaba88b8442077e2957e69303' /></div> 
  <table> 
    <tr> 
      <td> 
        <label for="id_first_name">First name</label>:
      </td> 
      <td> 
        <input name="first_name" value="Patrick" maxlength="30" type="text" id="id_first_name" size="30" /> 
      </td> 
    </tr> 
    <tr> 
      <td> 
        <label for="id_last_name">Last name</label>:
      </td> 
      <td> 
        <input name="last_name" value="Sung" maxlength="30" type="text" id="id_last_name" size="30" /> 
      </td> 
    </tr> 
  </table> 
  <input type="hidden" name="form_id" value="name_change_form" /> 
</form> 

ajax 实现工作正常。但我收到警告

资源解释为文档,但使用 MIME 类型 application/json 传输

在 Chrome 开发者工具中使用 MIME 类型 application/json 进行传输。我想找出为什么会出现警告,或者更好的是解决它的方法。

我改为使用 $.post ,神奇的是,从那时起错误就消失了。我不知道为什么 $.post 有效,但 $form.ajaxSubmit 无效。如果有人能提供他们的解释那就太好了。至少,这个问题已经解决了。以下是新代码。

var url = $form.attr("action");
$.post(
  url, 
  $form.serialize(), 
  function(data) {
    alert("success");
  },
  "json"
); 

I have the following snippet, which uses the jQuery Form plugin to post a form to the server (in ajax).

  var options = {
    dataType: "json",
    success: function(data) { 
      alert("success");
    } 
  }; 

  $form.ajaxSubmit(options);

The form:

<form enctype="multipart/form-data" id="name_change_form" method="post" action="/my_account/"> 
<div style='display:none'><input type='hidden' name='csrfmiddlewaretoken' value='6c9b552aaba88b8442077e2957e69303' /></div> 
  <table> 
    <tr> 
      <td> 
        <label for="id_first_name">First name</label>:
      </td> 
      <td> 
        <input name="first_name" value="Patrick" maxlength="30" type="text" id="id_first_name" size="30" /> 
      </td> 
    </tr> 
    <tr> 
      <td> 
        <label for="id_last_name">Last name</label>:
      </td> 
      <td> 
        <input name="last_name" value="Sung" maxlength="30" type="text" id="id_last_name" size="30" /> 
      </td> 
    </tr> 
  </table> 
  <input type="hidden" name="form_id" value="name_change_form" /> 
</form> 

The ajax implementation is working fine. But I am getting a warning

Resource interpreted as Document but transferred with MIME type application/json

in Chrome Developer Tools. I want to find out why the warning, or even better, a way to resolve it.

I changed to use $.post instead and magically the error was gone since then. I have no idea why $.post works but not $form.ajaxSubmit. If someone can offer their explanation that would be great. At the very least, this problem is resolved. Below is the new code.

var url = $form.attr("action");
$.post(
  url, 
  $form.serialize(), 
  function(data) {
    alert("success");
  },
  "json"
); 

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

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

发布评论

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

评论(7

南烟 2024-12-04 13:24:33

我面临着同样的错误。对我有用的解决方案是:

从服务器端,返回JSON响应时,更改content-type:text/html

现在浏览器(Chrome、Firefox 和 IE8)不会给出错误。

I was facing the same error. The solution that worked for me is:

From the server end, while returning JSON response, change the content-type: text/html

Now the browsers (Chrome, Firefox and IE8) do not give an error.

め可乐爱微笑 2024-12-04 13:24:33

此类警告通常会因请求 HTTP 标头而被标记。
特别是 Accept 请求标头。
HTTP 标头的 MDN 文档指出

接受请求 HTTP 标头通告客户端能够理解的内容类型(以 MIME 类型表示)。使用内容协商,服务器然后选择其中一个建议,使用它并通过 Content-Type 响应标头通知客户端其选择。浏览器根据请求完成的上下文为此标头设置足够的值...

application/json 可能不在浏览器发送的 Accept 标头中的 MIME 类型列表中,因此会出现警告。

解决方案

自定义 HTTP 标头只能通过 XMLHttpRequest 或任何实现它的 js 库包装器以编程方式发送。

This type of warnings are usually flagged because of the request HTTP headers.
Specifically the Accept request header.
The MDN documentation for HTTP headers states

The Accept request HTTP header advertises which content types, expressed as MIME types, the client is able to understand. Using content negotiation, the server then selects one of the proposals, uses it and informs the client of its choice with the Content-Type response header. Browsers set adequate values for this header depending of the context where the request is done....

application/json is probably not on the list of MIME types in the Accept header sent by the browser hence the warning.

Solution

Custom HTTP headers can only be sent programmatically via XMLHttpRequest or any of the js library wrappers implementing it.

你爱我像她 2024-12-04 13:24:33

这实际上是 Chrome 的一个怪癖,而不是 JavaScript 库。解决方法如下:

为了防止出现该消息并允许 chrome 在控制台中以 JSON 形式很好地呈现响应,请将查询字符串附加到您的请求 URL。

例如

var xhr_object = new XMLHttpRequest();

var url = 'mysite.com/'; // Using this one, Chrome throws error

var url = 'mysite.com/?'; // Using this one, Chrome works

xhr_object.open('POST', url, false);

It's actually a quirk in Chrome, not the JavaScript library. Here's the fix:

To prevent the message appearing and also allow chrome to render the response nicely as JSON in the console, append a query string to your request URL.

e.g

var xhr_object = new XMLHttpRequest();

var url = 'mysite.com/'; // Using this one, Chrome throws error

var url = 'mysite.com/?'; // Using this one, Chrome works

xhr_object.open('POST', url, false);
梦途 2024-12-04 13:24:33

我采取了不同的方法。我改用 $.post 并且从那时起错误就消失了。

I took a different approach. I switched to use $.post and the error has gone since then.

春夜浅 2024-12-04 13:24:33

这发生在我身上,一旦我删除了这个:enctype =“multipart / form-data”
它在没有警告的情况下开始工作

This happened to me, and once I removed this: enctype="multipart/form-data"
It started working without the warning

っ左 2024-12-04 13:24:33

您可以简单地使用 JSON.stringify(options) 在提交之前将 JSON 对象转换为字符串,然后警告关闭并且工作正常

you can simply use JSON.stringify(options) convert JSON object to string before submit, then warning dismiss and works fine

書生途 2024-12-04 13:24:33

使用数据类型:“jsonp”。我之前也遇到过同样的错误。它为我修好了。

Use dataType: "jsonp". I had the same error before. It fixed for me.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文