$.ajax - 数据类型 [英] $.ajax - dataType

查看:32
本文介绍了$.ajax - 数据类型的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

有什么区别

contentType: "application/json; charset=utf-8",
dataType: "json",

对比

contentType: "application/json",
dataType: "text",

推荐答案

  • contentType 是发送到服务器的 HTTP 标头,指定特定格式.
    示例:我要发送 JSON 或 XML
  • dataType 你是在告诉 jQuery 期望什么样的响应.
    需要 JSON、XML 或 HTML 等.默认是让 jQuery 尝试解决.
    • contentType is the HTTP header sent to the server, specifying a particular format.
      Example: I'm sending JSON or XML
    • dataType is you telling jQuery what kind of response to expect.
      Expecting JSON, or XML, or HTML, etc. The default is for jQuery to try and figure it out.
    • $.ajax() 文档有完整的描述还有这些.

      The $.ajax() documentation has full descriptions of these as well.

      在您的特定情况下,第一个是要求 响应UTF-8 中,第二个不在乎.同样,第一个将 response 视为 JavaScript 对象,第二个将其视为字符串.

      In your particular case, the first is asking for the response to be in UTF-8, the second doesn't care. Also the first is treating the response as a JavaScript object, the second is going to treat it as a string.

      所以第一个是:

      success: function(data) {
        // get data, e.g. data.title;
      }
      

      第二个:

      success: function(data) {
        alert("Here's lots of data, just a string: " + data);
      }
      

      这篇关于$.ajax - 数据类型的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆