jquery,ajax,jsonp例子+springmvc后台
<script src="js/jquery-1.11.1.min.js"></script> <script> $(function () { $("#username").bind('change', function () { /* $.ajax({ url: "http://localhost:8080/springmvc-01/checkusername.action", type: "POST", data: JSON.stringify({ 'username': $(this).val() }),//不用JSON.stringify容易报错,要传json字符串不是js对象或json字符串'{"username":'+$(this).val()+'}' dataType: 'json', contentType: "application/json", beforeSend: function () { //alert("this is before send"); }, success: function (data) { var hasSame = data.result; if (hasSame === 'true') { $('#nametips').html('<span style="color:red">用户名重复</span>'); } else { $('#nametips').html('<span style="color:green">可以使用</span>'); } }, error: function () { alert('error'); } });*/ /*ctrl+alt+l格式化代码*/ $.ajax({ type: "get", async: false, url: "http://localhost:8080/springmvc-01/checkusernamejsonp.action", dataType: "jsonp", data: JSON.stringify({ 'username': $(this).val() }),//不用JSON.stringify容易报错 jsonp: "callback",//传递给请求处理程序或页面的,用以获得jsonp回调函数名的参数名(默认为:callback) jsonpCallback: "aa",//自定义的jsonp回调函数名称,默认为jQuery自动生成的随机函数名 success: function (data) { var hasSame = data.result; if (hasSame === 'true') { $('#nametips').html('<span style="color:red">用户名重复</span>'); } else { $('#nametips').html('<span style="color:green">可以使用</span>'); } }, error: function () { alert('fail'); } }); }); }); </script> /**Jsonp跨域 * @param user * @param callback * @return */ @RequestMapping(value="/checkusernamejsonp") @ResponseBody public JSONPObject checkusernameJsonP(User user,String callback){ for (User iUser : userSet) { if(iUser.getUsername().equalsIgnoreCase(user.getUsername())){ return new JSONPObject(callback, "{\"result\":\"true\"}"); } } return new JSONPObject(callback, "{\"result\":\"false\"}"); } //返回值任意,转成json格式 /**ajax * @param user * @param model * @return */ @RequestMapping(value="/checkusername") @ResponseBody public String checkusername(@RequestBody User user,Model model){ for (User iUser : userSet) { if(iUser.getUsername().equalsIgnoreCase(user.getUsername())){ return "{\"result\":\"true\"}"; } } return "{\"result\":\"false\"}"; } jackson三个jar包: jackson-annotations-2.4.0.jar jackson-core-2.4.2.jar jackson-databind-2.4.2.jar 可以参考:http://blog.csdn.net/caiwenfeng_for_23/article/details/45300739
相关推荐
fengchao000 2020-06-17
adonislu 2020-05-16
zmosquito 2020-05-10
adonislu 2020-05-10
somebodyoneday 2020-04-22
fengchao000 2020-04-22
fengchao000 2020-04-11
Richardxx 2020-03-07
somebodyoneday 2020-03-06
fengchao000 2020-03-05
somebodyoneday 2020-02-16
xiaouncle 2020-02-13
baijinswpu 2020-01-29
fengchao000 2020-01-10
fengchao000 2019-12-25
newthon 2019-12-23
somebodyoneday 2013-07-11