首页 > 代码库 > Angular和jQuery的ajax请求的差别
Angular和jQuery的ajax请求的差别
|
|
Sending Data to the ServerBy default, Ajax requests are sent using the GET HTTP method. If the POST method is required, the method can be specified by setting a value for the The |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 | <!DOCTYPE html> < html > < head > < title ></ title > < script src = "js/jquery.js" ></ script > < script src = "js/angular.js" ></ script > </ head > < body ng-app = "myApp" > < div > < h1 >Hello World</ h1 > </ div > < div > < span >Angular ajax:</ span > < a href = "#" ng-controller = "btnCtrl" ng-click = "asave()" >Button</ a > </ div > < div > < span >jQuery ajax:</ span > < a href = "#" id = "jBtn" >Button</ a > </ div > < div > < span >Angular as jQuery ajax:</ span > < a href = "#" ng-controller = "btnCtrl" ng-click = "ajsave()" >Button</ a > </ div > </ body > < script src = "js/index.js" ></ script > </ html > |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 | var myApp = angular.module( ‘myApp‘ ,[]); var btnCtrl = myApp.controller( ‘btnCtrl‘ ,[ ‘$scope‘ , ‘$http‘ , function ($scope,$http){ $scope.asave = function (){ var user = { name : ‘zhangsan‘ , id : ‘3‘ } $http({method: ‘POST‘ ,url: ‘/asave‘ ,data:user}).success( function (data){ console.log(data); }) }; $scope.ajsave = function (){ var data = ‘namelisi&id=4‘ $http({ method: ‘POST‘ , url: ‘ajsave‘ , data: data, // pass in data as strings headers: { ‘Content-Type‘ : ‘application/x-www-form-urlencoded; charset=UTF-8‘ } }).success( function (data) { console.log(data); }); }; }]); $( ‘#jBtn‘ ).on( ‘click‘ , function (){ $.ajax({ type : ‘POST‘ , url : ‘jsave‘ , data : {name: ‘wangwu‘ ,id: ‘5‘ }, dataType: ‘json‘ , success : function (data){ console.log(data); } }) }); |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 | public class User { public String name; public String id; public String getName() { return name; } public void setName(String name) { this .name = name; } public String getId() { return id; } public void setId(String id) { this .id = id; } } |
1 2 3 4 5 6 7 | @RequestMapping ( "/asave" ) @ResponseBody public String asave( @RequestBody User user){ System.out.println( "name---" +user.getName()); System.out.println( "id---" +user.getId()); return "ok" ; } |
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 | @Controller public class MyController { @RequestMapping ( "/test" ) @ResponseBody public String test(){ return "hello world" ; } @RequestMapping ( "/asave" ) @ResponseBody public String asave( @RequestBody User user){ System.out.println( "name---" +user.getName()); System.out.println( "id---" +user.getId()); return "ok" ; } @RequestMapping ( "/jsave" ) @ResponseBody public String jsave( @RequestParam String name, @RequestParam String id){ System.out.println( "name---" +name); System.out.println( "id---" +id); return "ok" ; } @RequestMapping ( "/ajsave" ) @ResponseBody public String ajsave( @RequestParam String name, @RequestParam String id){ System.out.println( "name---" +name); System.out.println( "id---" +id); return "ok" ; } } |
Angular和jQuery的ajax请求的差别