jquery笔记

一、map与each的使用

 1 <html>
 2 <head>
 3 <meta charset="utf-8"> 
 4 <title>jquerydemo</title> 
 5 <script src="https://cdn.bootcss.com/jquery/1.10.2/jquery.min.js">
 6 </script>
 7 <script>
 8 $(document).ready(function(){
 9   $("input").each(function(index, item){
10       alert($(item).val());
11   });
12    $("input").map(function(){
13       alert($(this).val());
14   });
15 });
16 </script>
17 </head>
18 <body>
19 <input type="text" value="1"/>
20 <input type="text" value="2"/>
21     <input type="text" value="3"/>
22     <input type="text" value="4"/>
23 </body>
24 </html>

 二、ajax

    $(function () {
        $.ajax({
            type: "GET",
            url: "some",
            data: "name=John&location=Boston",
            success: function(msg){
                alert( "Data Saved: " + msg.name );
                alert( "Data Saved: " + msg.location );
            }
        });
    });
    $(function () {
        $.ajax({
            type: "POST",
            url: "some",
            data: "name=John&location=Boston",
            success: function(msg){
                alert( "Data Saved: " + msg.name );
                alert( "Data Saved: " + msg.location );
            }
        });
    });
 
@RequestMapping("/some")
    @ResponseBody
    public Map<String, Object> getSome(String name, String location) throws IOException {
        System.out.println("GET");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name",name);
        map.put("location",location);
        return map;
    }
    @RequestMapping(value = "/some",method = RequestMethod.POST)
    @ResponseBody
    public Map<String, Object> postSome(String name, String location) throws IOException {
        System.out.println("POST");
        Map<String,Object> map = new HashMap<String,Object>();
        map.put("name",name);
        map.put("location",location);
        return map;
    }

 三、JSON.parse()和JSON.stringify()区别

JSON.parse()【从一个字符串中解析出json对象】

例子:

//定义一个字符串

var data='{"name":"goatling"}'

//解析对象​ 就是将json字符串转换为对象

​JSON.parse(data)

结果是:

​name:"goatling"

JSON.stringify()【从一个对象中解析出字符串】 说白了就是将json对象转换为字符串

var data={name:'goatling'}

JSON.stringify(data)

结果是:

'{"name":"goatling"}'

 

posted @ 2018-08-17 15:37  四季写爱  阅读(186)  评论(0编辑  收藏  举报