欢迎来到科站长!

AJAX相关

当前位置: 主页 > 网络编程 > AJAX相关

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

时间:2018-10-30 09:38:48|栏目:AJAX相关|点击:

这篇文章主要介绍了$.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报错400的参考解决办法

栏    目:AJAX相关

下一篇:解决ajax异步请求返回的是字符串问题

本文标题:$.ajax中contentType: “application/json” 的用法详解

本文地址:https://www.fushidao.cc/wangluobiancheng/255.html

广告投放 | 联系我们 | 版权申明

申明:本站所有的文章、图片、评论等,均由网友发表或上传并维护或收集自网络,属个人行为,与本站立场无关。

如果侵犯了您的权利,请与我们联系,我们将在24小时内进行处理、任何非本站因素导致的法律后果,本站均不负任何责任。

联系QQ:257218569 | 邮箱:257218569@qq.com

Copyright © 2018-2025 科站长 版权所有冀ICP备14023439号