微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

jquery – 资源解释为Document但在Chrome Developer Tools中使用MIME类型application / json警告进行传输

我有以下代码片段,它使用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实现工作正常.但是我收到了警告

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

在Chrome开发者工具中.我想找出为什么警告,甚至更好的解决方法.

我改为使用$.post而神奇地从那时起错误消失了.我不知道为什么$.post工作但不是$form.ajaxSubmit.如果有人可以提供他们的解释,那将是伟大的.至少,这个问题已得到解决.以下是新代码.

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

解决方法

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

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

现在浏览器(Chrome,Firefox和IE8)不会出错.

原文地址:https://www.jb51.cc/jquery/181103.html

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。

相关推荐