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实现的,但当我进行局部刷新的时候,页面并没有刷新和响应, 在后台的代码中打了断点也并没有 ...
随机推荐
- UWP 使用Telerik Grid控件
还是老规矩,看一下最终效果. 数据是从SQLite中读取,然后绑定到DataGrid中显示的. 先看一下XAML <grid:RadDataGrid Grid.Row="1" ...
- Bootstrap 在手机页时,导航下拉自动回收
$(".menu-main").collapse("hide"); //.menu-main就是下来导航的类名
- vscode前端常用插件推荐,搭建JQuery、Vue等开发环境
vscode是微软开发的的一款代码编辑器,就如官网上说的一样,vscode重新定义(redefined)了代码编辑器.当前市面上常用的轻型代码编辑器主要是:sublime,notepad++,edit ...
- Problem : (1.2.1) Text Reverse
#include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...
- JVM内存越多,能创建的线程越少,越容易发生java.lang.OutOfMemoryError: unable to create new native thread。
一.认识问题: 首先我们通过下面这个 测试程序 来认识这个问题:运行的环境 (有必要说明一下,不同环境会有不同的结果):32位 Windows XP,Sun JDK 1.6.0_18, eclipse ...
- UML系列图2
时序图: 用例图:
- Java测试(一)
关于while和do-while循环,下列说法正确的是 A 两种循环除了格式不同外,功能完全相同 B 与do-while语句不通的是,while语句的循环至少执行一次 C do-while语句首 ...
- javascript里的循环语句
前序:我一直对于for跟for..in存在一种误解,我觉得for都能把事情都做了,为啥还要for...in...这玩意了,有啥用,所以今天就说说JavaScript里的循环语句. 循环 要计算1+2+ ...
- Konckout第四个实例:组合类型数据绑定 -- 日期双向绑定显示
<!doctype html> <html > <head> <meta http-equiv="Content-Type" conten ...
- [bzoj1601]灌水_kruskal
灌水 bzoj-1601 题目大意:给你n块地,将两块地之间连通有代价$P_{i,j}$,单独在一块地打井需要代价$C_i$,问将所有的井都有水的代价是多少. 注释:1<=n<=300. ...