最近在做项目交互的时候,刚开始向后台传递数据返回,后来百度添加了 contentType:"application/json" 之后返回,然后把传输的数据格式改为json字符串就传输成功了,现在我们来看看 contentType:“application/json“的作用:

添加 contentType:“application/json“之后,向后台发送数据的格式必须为json字符串

  1. $.ajax({
  2. type: "post",
  3. url: "mobile/notice/addMessageInfo.jspx",
  4. contentType: "application/json",
  5. data:"{'name':'zhangsan','age':'15'}",
  6. dataType: "json",
  7. success: function(data) {
  8. console.log(data);
  9. },
  10. error: function(msg) {
  11. console.log(msg)
  12. }
  13. })

不添加 contentType:“application/json“的时候可以向后台发送json对象形式

  1. $.ajax({
  2. type: "post",
  3. url: "mobile/notice/addMessageInfo.jspx",
  4. data:{name:'zhangsan',age:'15'},
  5. dataType: "json",
  6. success: function(data) {
  7. console.log(data);
  8. },
  9. error: function(msg) {
  10. console.log(msg)
  11. }
  12. })

另外,当向后台传递复杂json的时候,同样需要添加 contentType:“application/json“,然后将数据转化为字符串

  1. var data = {
  2. uploadarray: uploadarray,
  3. messageInfo: {
  4. messageTitle: messageTitle,
  5. messageContent: messageContent,
  6. publisher: publisher
  7. },
  8. userId: userId
  9. }
  10.  
  11. $.ajax({
  12. type: 'post',
  13. url: "mobile/notice/addMessageInfo.jspx",
  14. contentType: 'application/json',
  15. data: JSON.stringify(data),
  16. dataType: "json",
  17. success: function(data) {
  18. console.log(data);
  19. },
  20. error: function(msg) {
  21. console.log(msg)
  22. }
  23. })

ajax中设置contentType: “application/json”的作用的更多相关文章

  1. ajax 发送json数据时为什么需要设置contentType: "application/json”

    1. ajax发送json数据时设置contentType: "application/json”和不设置时到底有什么区别? contentType: "application/j ...

  2. ajax发送json数据时为什么需要设置contentType: "application/json”

    1. ajax发送json数据时设置contentType: "application/json”和不设置时到底有什么区别?contentType: "application/js ...

  3. 处理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 ...

  4. [转] $.ajax中contentType: “application/json” 的用法

    不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...

  5. WebForm下的$.ajax中contentType: “application/json” 的用法

    不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...

  6. $.ajax中contentType: “application/json” 的用法

    不使用contentType: “application/json”则data可以是对象 $.ajax({ url: actionurl, type: "POST", datTyp ...

  7. $.ajax data向后台传递参数失败 contentType: "application/json"

    在ajax方法设置中若不添加 contentType: "application/json" 则data可以是对象: $.ajax({ url: actionurl, type: ...

  8. content-type: application/json没有设置导致的500错误

    $.ajax({ url:'http://xxx.test', type: 'Post', data: JSON.stringify(model), dataType: 'json', content ...

  9. ajax中的contentType使用

    本文为博主原创,未经允许不得转载: 最近在修改部分项目功能的时候,遇到一个问题.局部刷新某页面的功能是由ajax实现的,但当我进行局部刷新的时候,页面并没有刷新和响应, 在后台的代码中打了断点也并没有 ...

随机推荐

  1. UWP 使用Telerik Grid控件

    还是老规矩,看一下最终效果. 数据是从SQLite中读取,然后绑定到DataGrid中显示的. 先看一下XAML <grid:RadDataGrid Grid.Row="1" ...

  2. Bootstrap 在手机页时,导航下拉自动回收

    $(".menu-main").collapse("hide"); //.menu-main就是下来导航的类名

  3. vscode前端常用插件推荐,搭建JQuery、Vue等开发环境

    vscode是微软开发的的一款代码编辑器,就如官网上说的一样,vscode重新定义(redefined)了代码编辑器.当前市面上常用的轻型代码编辑器主要是:sublime,notepad++,edit ...

  4. Problem : (1.2.1) Text Reverse

    #include<iostream> using namespace std; void main() { char arr[1000]; int a,n; int s,t; cin> ...

  5. JVM内存越多,能创建的线程越少,越容易发生java.lang.OutOfMemoryError: unable to create new native thread。

    一.认识问题: 首先我们通过下面这个 测试程序 来认识这个问题:运行的环境 (有必要说明一下,不同环境会有不同的结果):32位 Windows XP,Sun JDK 1.6.0_18, eclipse ...

  6. UML系列图2

    时序图: 用例图:

  7. Java测试(一)

    关于while和do-while循环,下列说法正确的是  A 两种循环除了格式不同外,功能完全相同  B 与do-while语句不通的是,while语句的循环至少执行一次  C do-while语句首 ...

  8. javascript里的循环语句

    前序:我一直对于for跟for..in存在一种误解,我觉得for都能把事情都做了,为啥还要for...in...这玩意了,有啥用,所以今天就说说JavaScript里的循环语句. 循环 要计算1+2+ ...

  9. Konckout第四个实例:组合类型数据绑定 -- 日期双向绑定显示

    <!doctype html> <html > <head> <meta http-equiv="Content-Type" conten ...

  10. [bzoj1601]灌水_kruskal

    灌水 bzoj-1601 题目大意:给你n块地,将两块地之间连通有代价$P_{i,j}$,单独在一块地打井需要代价$C_i$,问将所有的井都有水的代价是多少. 注释:1<=n<=300. ...