欢迎各位兄弟 发布技术文章

这里的技术是共享的

You are here

前端开发学习:contentType: “application/json” 的理解和应用 有大用

前端开发学习:contentType: “application/json” 的理解和应用。

  1. $.ajax({

  2.   type: httpMethod,

  3.   cache:false,

  4.   async:false,

  5.   contentType: "application/json; charset=utf-8",

  6.   dataType: "json",//返回值类型

  7.   url: path+url,

  8.   data:jsonData,

  9.   success: function(data){

  10.     var resultData = '返回码='+data.status+',响应结果='+data.message+',耗时='+data.tcost;

  11.     layer.msg(resultData,{icon: 1});

  12.   },

  13.   error : function(xhr, ts, et) {

  14.     layer.msg('服务调用失败!', {icon: 2});

  15.   }

  16. });

contentType: 发送信息至服务器时内容编码类型,简单说告诉服务器请求类型的数据。

 默认值: "application/x-www-form-urlencoded"。

dataType:告诉服务器,我要想什么类型的数据,除了常见的json、XML,还可以指定 html、jsonp、script或者text

不使用contentType: “application/json”则data可以是对象。

  1. $.ajax({

  2. url: actionurl,

  3. type: "POST",

  4. datType: "JSON",

  5. data: { id: nodeId },

  6. async: false,

  7. success: function () {}

  8. });

使用contentType: “application/json”则data只能是json字符串。

  1. $.ajax({

  2. url: actionurl,

  3. type: "POST",

  4. datType: "JSON",

  5. contentType: "application/json"

  6. data: "{'id': " + nodeId +"}",

  7. async: false,

  8. success: function () {}

  9. });


来自  https://www.jiangweishan.com/article/js20201228a1.html


https://www.jiangweishan.com/article/js20201228a1.html


普通分类: