关于 ajax Content-Type 的问题 贼拉有用的!!!
2017-12-19 10:30
295 查看
之前我写ajax根本就没有去关注响应头什么的 只是很简单的用而已 后来发现其实没那么简单
Content-Type 指的是发送至服务器的数据类型,而data-Type定义的是服务器返回的数据类型.此处应有图
上面那个红圈是Content-Type控制的数据类型 ,下面那个红圈是data-Type控制的数据类型
如果不指定ContentType,则一般情况默认为text/html,表单数据默认为application/x-www-form-urlencoded。
如果是 type: “GET”的话,以上三种都可以,因为GET类型的参数是紧跟在url后面,与Content-Type无关如果是 type: “POST”的话,data数据是由form表单提交,此时就必须把Content-Type设为application/x-www-form-urlencoded(也可以不指定content-Type,因为表单提交默认是application/x-www-form-urlencoded),否则后台是无法接收到数据的。
contentType数据类型的不同 data里面所传递的数据类型也不同
如果是不使用contentType: “application/json”则data可以是对象*
$.ajax({ type: "GET", url: "/SmartStrip/InsertRow/GetDeviceList", data: { }, dataType: 'JSON', success: function (data) { console.log(data); }, error: function () { }, })
如果是使用contentType: “application/json”则data只能是json形式的字符串,需自行拼接
$.ajax({ type: "POST", beforeSend: function (request) { request.setRequestHeader("Authorization",Authorization); }, url: url, dataType: 'JSON', contentType: "application/json", data: '{' + '"name":"' + name + '",' + '"idsn":"' + idsn + '",' + '"brand":"' + brand + '",' + '"model":"' + model + '",' + '"pid":"' + pid + '",' + '"baseID":"' + baseID + '",' + '"areaID":"' + areaID + '"' + '}', success: function (data) { console.log(data); }, error: function () { }, })
哇咔咔 ~~ 写完了~
相关文章推荐
- 关于Ajax开发中Response的Content-Type的一些问题
- 关于ajax 上传图片 enctype="multipart/form-data"的问题
- 关于读取Excel文件报错的问题(Package should contain a content type part [M1.13])
- fetch 关于七牛的content-type 的问题
- 关于ajax 上传图片 enctype="multipart/form-data"的问题
- 关于ajaxFileUpload造成 input[type=file] change事件只能触发一次的问题
- 关于response.setContentType("text/html; charset=UTF-8");乱码问题
- 关于ajax方法的dataType属性和成功后回调函数遇到的几点问题
- 关于ajax的dataType类型为text返回值乱码的问题
- 关于Ajax的type为post提交方式出现请求失效问题
- 关于请求头Content-type的取值问题
- 关于出现Specified VM install not found: type Standard VM, name jdk1.5.0_04问题的解决办法
- 关于ajax的问题
- 关于Ajax的参数值中文乱码问题
- 关于使用bootstrap中modal和ajaxupload.js图片问题
- 关于ajax的本地请求json文件的问题
- 关于ajaxSubmit使用问题
- 解决content-type为"application/json"的post过来的数据在php端接受不到的问题
- 关于springmvc,post请求的一点问题(415 unsupported media type,400,接不到参数等)
- 关于Chrome报“Application Cache Error event: Invalid manifest mime type (application/x-ms-manifest)”错误的问题