ajax请求里的contentType: "application/json"作用

对于页面中复杂数据的提交:

$.ajax({

type:"post",

url:url,

dataType: "json",

contentType: "application/json",

async:true,

data:JSON.stringify(param),

success:function(result){

if(result.code == "0"){

var rows = result.data;

if(typeof successcallback == ‘function‘){successcallback(rows);}else{alert(result.msg);}

}else{

alert(result.msg);

}

},

error:function(jqXHR, textStatus, errorThrown){

//alert("提交失败,请重试!");

}

});

需要通过JSON.stringify(param)把页面中获取的参数变成json字符串,,此时需要加上contentType: "application/json",

时间: 2024-12-14 08:18:45

ajax请求里的contentType: "application/json"作用的相关文章

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

1. ajax发送json数据时设置contentType: "application/json”和不设置时到底有什么区别?contentType: "application/json”,首先明确一点,这也是一种文本类型(和text/json一样),表示json格式的字符串,如果ajax中设置为该类型,则发送的json对象必须要使用JSON.stringify进行序列化成字符串才能和设定的这个类型匹配.同时,对应的后端如果使用了Spring,接收时需要使用@RequestBody来注解

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

1. ajax发送json数据时设置contentType: "application/json"和不设置时到底有什么区别? contentType: "application/json",首先明确一点,这也是一种文本类型(和text/json一样),表示json格式的字符串,如果ajax中设置为该类型,则发送的json对象必须要使用JSON.stringify进行序列化成字符串才能和设定的这个类型匹配.同时,对应的后端如果使用了Spring,接收时需要使用@Req

C# ContentType: "application/json" 请求方式传json参数问题

处理Http请求时遇到的ContentType为application/json方式,记录下这种Post请求方式下如何传json参数: var request = (HttpWebRequest)WebRequest.Create("http://url"); request.ContentType = "application/json"; request.Method = "POST"; using (var streamWriter = n

ajax中设置contentType: "application/json",后端参数如何接收

javaScript部分 这里contentType: "application/json还未设置 1. function testRequestBody() { var book = new Object(); var id = 1; var name = "Spring MVC企业应用实践"; book.id = 1; book.name = "Spring MVC企业应用实践"; $.ajax({ url:"${pageContext.re

PHP使用CURL设置header头传参以及设置Content-Type: application/json类型的后台数据接收

CURL函数 public function CurlRequest($url,$data=null,$header=null){ //初始化浏览器 $ch = curl_init(); //设置浏览器,把参数url传到浏览器的设置当中 curl_setopt($ch, CURLOPT_URL, $url); //以字符串形式返回到浏览器当中 curl_setopt($ch, CURLOPT_RETURNTRANSFER, true); //禁止https协议验证域名,0就是禁止验证域名且兼容p

http post Content-type: application/json; charset=utf-8

  The header just denotes what the content is encoded in. It is not necessarily possible to deduce the type of the content from the content itself, i.e. you can't necessarily just look at the content and know what to do with it. That's what HTTP head

ajaxfileupload.js ajax上传文件(含application/json)

jQuery.extend({ createUploadIframe: function(id, uri) { //create frame var frameId = 'jUploadFrame' + id; if(window.ActiveXObject) { var io = document.createElement('<iframe id="' + frameId + '" name="' + frameId + '" />'); if(ty

springboot+shiro 02 - 异步ajax请求无权限时,返回json格式数据

博客: https://www.cnblogs.com/youxiu326/p/shiro-01.html github:https://github.com/youxiu326/sb_shiro_session.git 在原有基础上添加 SimpleFormAuthenticationFilter /** * 自定义过滤器,ajax请求数据 以json格式返回 * Created by lihui on 2019/2/28. */ public class SimpleFormAuthenti

ajax请求学习之&lt;XMLHttpRequestJSON&gt; -&gt;以json格式传输数据

以json格式传输数据 <!DOCTYPE HTML> <html> <head> <meta http-equiv="Content-Type" content="text/html; charset=utf-8"> <title>Demo</title> <style> body, input, select, button, h1 { font-size: 28px; line