AJAX请求中contentType和dataType的区别

contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8')

           告诉服务器,我要发什么类型(格式)的数据

dataType (default: Intelligent Guess (xml, json, script, or html))

           告诉服务器,我要想什么类型(格式)的数据

contentType:

          application/json

          application/x-www-form-urlencoded(默认)

          text/plain

          multipart/form

 

        如ajax 请求时不设置任何contentType,默认将使用contentType: "application/json”application/x-www-form-urlencoded,这种格式的特点就是,name/value 成为一组,

        每组之间用 & 联接,而 name与value 则是使用 = 连接。如: www.baidu.com/query?user=username&pass=password 这是get请求, 而 post 请求则是使用请求体,参数不在 url 中,在请求体中的参数表现形式也是: user=username&pass=password的形式。使用这种contentType时,对于简单的json对象类型,如:{“a”:1,"b":2,"c":3} 这种,将也会被转成user=username&pass=password 这种形式发送到服务端。而服务端接收时就按照正常从from表单中接收参数那样接收即可,不需设置@RequestBody之类的注解。

       但对于复杂的json 结构数据,这种方式处理起来就相对要困难,服务端解析时也难以解析,所以,就有了application/json 这种类型,这是一种数据格式的申明,明确告诉服务端是什么格式的数据,服务端只需要根据这种格式的特点来解析数据即可。

        application/json表示json格式的字符串,一旦contetnType=application/json,则表示向服务器发送序列后的json字符串.后台也必须使用@RequestBody来解析并绑定json字符串参数到方法入参。

总结

        1).ajax 如果发送的是json字符串,服务端接收时必须要使用@RequestBody注解。始终记住,json字符串,"application/json”,@RequestBody 这三者之间是一一对应的,要有都有,要没有都没有。

        2).如果发送的是json对象,contentType不能设置为"application/json”,需使用默认的类型(application/x-www-form-urlencoded,为什么呢?这种类型最后还是会把json对象类型的参数转为user=username&pass=password这种形式后再发送,需要明白一点:这种转换时只能识别json对象类型,不能识别json字符串类型)。

 

JSON对象和JSON字符串的区别

 

说到对象的概念,对象的属性是可以用:对象.属性进行调用的

 var person={"name":"zhangsan","sex":"男","age":"24"}//json对象

字符串,我们常说的JavaScript中的字符串是单引号或者双引号引起来的

var person='{"name":"zhangsan","sex":"男","age":"24"}';//json字符串

总结:对象{}  字符串’ ’或者” ”引起来

JSON字符串和JOSN对象的转换

json字符串转json对象,调用parse方法:

var person='{"name":"zhangsan","sex":"男","age":"24"}';//json字符串

var personObject = JSON.parse(person);

alert(personObject.name);//zhangsan

json对象转为json字符串,调用stringify方法:

var person={"name":"zhangsan","sex":"男","age":"24"};//json对象

var personString = JSON.stringify(person);

alert(personString);

SpringMVC接受json字符串类型。

 

在SpringMVC中基于REST开发时,前端传入后台的应该是一个json格式的字符串,而不是一个json对象
 

<script type="text/javascript"> 

    $(document).ready(function(){ 

        var saveDataAry=[]; 

        var data1={"userName":"zhangsan","address":"bj"}; 

        var data2={"userName":"lisi","address":"nj"}; 

        saveDataAry.push(data1); 

        saveDataAry.push(data2);        

        $.ajax({

            type:"POST",

            url:"user/saveUser",

            dataType:"json",     

            contentType:"application/json",              

            data:JSON.stringify(saveData),

            success:function(data){



            }

         });

    }); 

</script>

上面代码,首先push方法将其封装到数组中,其表现格式:

 

[

    {"userName":"zhangsan","address":"bj"},

    {"userName":"lisi","address":"nj"}

]

JSON.stringify(saveData)将其转换为json字符串:同时ajax请求的时候也要指定dataType: “json”,contentType:”application/json” 这样就可以轻易的将一个对象或者List传到Java端。

 

java后台

@Controller

@RequestMapping(value = "saveUser", method=RequestMethod.POST )

@ResponseBody 

public void saveUser(@RequestBody List<User> users) {

    userService.batchSave(users);

}

 

后台用@RequestBody将其封装到List<User>中。然后进入Service层。

 

参考

https://blog.csdn.net/qq_39641912/article/details/79862914

https://blog.csdn.net/zsensei/article/details/80043764

https://blog.csdn.net/tycoon1988/article/details/40080691

猜你喜欢

转载自blog.csdn.net/qq_40531768/article/details/89286885
今日推荐