https://api.jquery.com/jquery.ajax/

What is content-type and datatype in an AJAX request?

contentType is the type of data you're sending, so application/json; charset=utf-8 is a common one, as is application/x-www-form-urlencoded; charset=UTF-8, which is the default.

dataType is what you're expecting back from the server: json, html, text, etc. jQuery will use this to figure out how to populate the success function's parameter.

If you're posting something like:

{"name":"John Doe"}

and expecting back:

{"success":true}

Then you should have:

var data = {"name":"John Doe"}
$.ajax({
dataType : "json",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
success : function(result) {
alert(result.success); // result is an object which is created from the returned JSON
},
});

If you're expecting the following:

<div>SUCCESS!!!</div>

Then you should do:

var data = {"name":"John Doe"}
$.ajax({
dataType : "html",
contentType: "application/json; charset=utf-8",
data : JSON.stringify(data),
success : function(result) {
jQuery("#someContainer").html(result); // result is the HTML text
},
});

One more - if you want to post:

name=John&age=34

Then don't stringify the data, and do:

var data = {"name":"John", "age": 34}
$.ajax({
dataType : "html",
contentType: "application/x-www-form-urlencoded; charset=UTF-8", // this is the default value, so it's optional
data : data,
success : function(result) {
jQuery("#someContainer").html(result); // result is the HTML text
},
});

$.ajax - dataType

  • contentType is the 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 it for jQuery to try and figure it out.

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

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.

So the first would be:

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

The second:

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

最新文章

  1. Hibernnate延迟加载策略(这么详细你还看不懂)
  2. SQL Server获取下一个编码字符实现继续重构与增强
  3. 网上图书商城3--Book模块
  4. lodash接触:string-capitalize
  5. 在magneto系统中输出tier price的最小值
  6. web.xml配置详解 (及&lt;context-param&gt;配置作用 )
  7. [置顶] 软件设计之道_读书纪要.doc
  8. 如何在MFC中操作资源句柄
  9. ApexSql Log
  10. typecho for SAE
  11. APP测试相关点归纳
  12. Kubernetes集群的监控报警策略最佳实践
  13. linux环境下,对于一个大文件,如何查看其中某行的内容
  14. host文件的作用
  15. loj#2353. 「NOI2007」 货币兑换 斜率优化
  16. 组合(composite)模式
  17. JAVA与DOM解析器基础 学习笔记
  18. CentOS7安装OpenStack(Rocky版)-01.控制节点的系统环境准备
  19. 009.KVM配置调整
  20. Flask中&#39;endpoint&#39;(端点)的理解

热门文章

  1. I、Mac 下的Vue
  2. c# Group类
  3. Mongodb之简介
  4. mysql:[Err] 1075 - Incorrect table definition; there can be only one auto column and it must be defined as a key
  5. amazeui datepicker日历控件 设置默认当日
  6. Codes: MODERN ROBOTICS Ch.3_Expo. Coods.基础代码实现
  7. 渗透之路基础 -- XXE注入漏洞
  8. React使用axios请求并渲染数据
  9. Python练习题---判断回文数
  10. 最快速的办法解决MySQL数据量增大之后翻页慢问题