$.ajax中contentType: “application/json” 的用法详解
这篇文章主要介绍了$.ajax中contentType: “application/json” 的用法,非常不错,具有一定的参考借鉴价值,需要的朋友可以参考下
具体内容如下所示:
$.ajax({ type: httpMethod, cache:false, async:false, contentType: "application/json; charset=utf-8", dataType: "json",//返回值类型 url: path+url, data:jsonData, success: function(data){ var resultData = '返回码='+data.status+',响应结果='+data.message+',耗时='+data.tcost; layer.msg(resultData,{icon: 1}); }, error : function(xhr, ts, et) { layer.msg('服务调用失败!', {icon: 2}); } });
区分:
contentType: 发送信息至服务器时内容编码类型,简单说告诉服务器请求类型的数据
默认值: "application/x-www-form-urlencoded"
dataType:告诉服务器,我要想什么类型的数据,除了常见的json、XML,还可以指定 html、jsonp、script或者text
不使用contentType: “application/json”则data可以是对象
$.ajax({ url: actionurl, type: "POST", datType: "JSON", data: { id: nodeId }, async: false, success: function () {} });
使用contentType: “application/json”则data只能是json字符串
$.ajax({ url: actionurl, type: "POST", datType: "JSON", contentType: "application/json" data: "{'id': " + nodeId +"}", async: false, success: function () {} });
栏 目:AJAX相关
本文标题:$.ajax中contentType: “application/json” 的用法详解
本文地址:https://www.fushidao.cc/wangluobiancheng/255.html
您可能感兴趣的文章
- 04-29在layer弹出层中通过ajax返回html拼接字符串填充数据的方法
- 04-29浅析IE浏览器关于ajax的缓存机制
- 04-29ajax异步读取后台传递回的下拉选项的值方法
- 07-03Ajax原理与应用案例快速入门教程
- 06-15解决Ajax方式上传文件报错"Uncaught TypeError: Illegal invocation"
- 06-13Ajax跨域问题及解决方案(jsonp,cors)
- 06-10SpringMVC+Jquery实现Ajax功能
- 05-29Ajax请求跨域问题解决方案分析
- 05-04关于Ajax跨域问题及解决方案详析
- 04-25Ajax登陆使用Spring Security缓存跳转到登陆前的链接

