ajax中设置contentType: “application/json”的作用
最近在做项目交互的时候,刚开始向后台传递数据返回,后来百度添加了 contentType:"application/json" 之后返回,然后把传输的数据格式改为json字符串就传输成功了,现在我们来看看 contentType:“application/json“的作用:
添加 contentType:“application/json“之后,向后台发送数据的格式必须为json字符串
$.ajax({
type: "post",
url: "mobile/notice/addMessageInfo.jspx",
contentType: "application/json",
data:"{'name':'zhangsan','age':'15'}",
dataType: "json",
success: function(data) {
console.log(data);
},
error: function(msg) {
console.log(msg)
}
})
不添加 contentType:“application/json“的时候可以向后台发送json对象形式
$.ajax({
type: "post",
url: "mobile/notice/addMessageInfo.jspx",
data:{name:'zhangsan',age:'15'},
dataType: "json",
success: function(data) {
console.log(data);
},
error: function(msg) {
console.log(msg)
}
})
另外,当向后台传递复杂json的时候,同样需要添加 contentType:“application/json“,然后将数据转化为字符串
var data = {
uploadarray: uploadarray,
messageInfo: {
messageTitle: messageTitle,
messageContent: messageContent,
publisher: publisher
},
userId: userId
} $.ajax({
type: 'post',
url: "mobile/notice/addMessageInfo.jspx",
contentType: 'application/json',
data: JSON.stringify(data),
dataType: "json",
success: function(data) {
console.log(data);
},
error: function(msg) {
console.log(msg)
}
})
ajax中设置contentType: “application/json”的作用的更多相关文章
- ajax 发送json数据时为什么需要设置contentType: "application/json”
1. ajax发送json数据时设置contentType: "application/json”和不设置时到底有什么区别? contentType: "application/j ...
- ajax发送json数据时为什么需要设置contentType: "application/json”
1. ajax发送json数据时设置contentType: "application/json”和不设置时到底有什么区别?contentType: "application/js ...
- 处理flutter http请求添加application/json报错Cannot set the body fields of a Request with content-type “application/json”
在flutter中在http请求发送时设置"content-type": "application/json"会出现报错Cannot set the body ...
- [转] $.ajax中contentType: “application/json” 的用法
不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...
- WebForm下的$.ajax中contentType: “application/json” 的用法
不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...
- $.ajax中contentType: “application/json” 的用法
不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...
- $.ajax data向后台传递参数失败 contentType: "application/json"
在ajax方法设置中若不添加 contentType: "application/json" 则data可以是对象: $.ajax({ url: actionurl, type: ...
- content-type: application/json没有设置导致的500错误
$.ajax({ url:'http://xxx.test', type: 'Post', data: JSON.stringify(model), dataType: 'json', content ...
- ajax中的contentType使用
本文为博主原创,未经允许不得转载: 最近在修改部分项目功能的时候,遇到一个问题.局部刷新某页面的功能是由ajax实现的,但当我进行局部刷新的时候,页面并没有刷新和响应, 在后台的代码中打了断点也并没有 ...
随机推荐
- html2canvas不能识别svg的解决方案
最新有个功能需要截取网页成图片,于是用到比较流行的html2canvas,本来以为能顺顺利利的搞定,后来发现网页上的流程图连接线不在截图中.于是各种百度.bing,也搜到好多,但是感觉没有一个完整的代 ...
- Java的大数计算BigNumber
Notice that the number 123456789 is a 9-digit number consisting exactly the numbers from 1 to 9, wit ...
- 直播-srs起步
srs简介 https://github.com/ossrs/srs/wiki/v2_CN_Home 原料 CentOS Linux release 7.2.1511 (Core) ffmpe ...
- Selenium webdriver实现截图功能
可参考http://www.cnblogs.com/tobecrazy/p/3599568.html Webdriver截图时,需要引入: import java.io.File; import ja ...
- python 对象和json互相转换
一.python对json的支持 从python2.6开始,python标准库中添加了对json的支持,操作json时,只需要import json即可. 二.python对象转换成json字符串 在 ...
- java中断
理解java中断 Java中断机制是一种协作机制,即通过中断并不能直接终止另一个线程,而需要被中断的线程自己处理中断.例如,当线程t1想中断线程t2,只需要在线程t1中将线程t2对象的中断标识置为tr ...
- Jdk1.7+eclipse搭建Java开发环境
Jdk1.7+eclipse搭建Java开发环境 1. 下载jdk1.7 http://www.oracle.com/technetwork/java/javase/downloads/jdk7 ...
- [HNOI2002]营业额统计_Treap
[HNOI2002]营业额统计 题目大意:给你一串n数序列,对于每一个刚输入的数a,找到一个前面的数k,使得|a-k|最小. 注释:$n<=32767,ai<=10^6$. 想法:刚学Tr ...
- bug单的生命周期
测试工程师发现了软件的缺陷(bug),那修复bug的整个流程是怎么样的呢? 1.发现bug 2.和开发确认是否是bug 3.如果是bug则提bug单到测试经理,如果不是则放过 4.测试经理把bug单走 ...
- GLSL Versions和GLSL ES Versions 对比
You can use the #version command as the first line of your shader to specify GLSL version: #version ...