ajax请求和angular js中http请求

来源:互联网 发布:打谱软件overture教程 编辑:程序博客网 时间:2024/06/05 00:17

1.ajax请求:

mui.plusReady(function() {var self = plus.webview.currentWebview();for(var i = 0; i < 5; i++) {var temp = {};var sub = plus.webview.create(subpages[i], subpages[i], subpage_style);if(i > 0) {sub.hide();} else {temp[subpages[i]] = "true";mui.extend(aniShow, temp);}self.append(sub);}plus.runtime.getProperty(plus.runtime.appid, function(inf) {wgtVer = inf.version;var _config = {url: 'http://60.30.254.100:8035/'//url: 'http://192.168.1.108:8080/'}var checkUrl = _config.url + "version/compareVersionIndexMain.action";var data = {"appversion": wgtVer,"id": "3"}$.ajax({type: "POST",url: checkUrl,dataType: "json",data: {ds: JSON.stringify(data)},success: function(data) {if(data.success == "0"){alert("已有新版本,请更新");}else if(data.success == "1"){}},error: function(e) {return;}});});});
注意:

第一点:plus-api代码要写在plus-ready之后

第二点:ajax请求要用JSON.stringify转一下,返回的数据data.success是String数据,与0或者1比较要用==

             java后台代码:

@RequestMapping("/compareVersionIndexMain")public void compareVersionIndexMain(HttpServletRequest req, HttpServletResponse res)throws UnsupportedEncodingException {req.setCharacterEncoding("utf-8");System.out.println("AAAAAAAAAAAA");String ds = req.getParameter("ds");System.out.println("ds==="+ds);JSONObject json = JSONObject.fromObject(ds);System.out.println("解析后的json数据===" + json.toString());String appversion = json.getString("appversion");String id = json.getString("id");JSONObject jsonObject = new JSONObject();if (id != null && appversion != null) {Version version = dao.getVersionById(Integer.parseInt(id));if (Float.parseFloat(appversion) < Float.parseFloat(version.getV_num())) {jsonObject.put("success", "0");jsonObject.put("url", version.getUrl());} else {jsonObject.put("success", "1");}} else {jsonObject.put("success", "-1");}System.out.println("首页检查更新返回的数据==="+jsonObject.toString());returnResponse(res, jsonObject.toString());}

第三点:plus-ready和plus-api位置,链接: MUI -- plus初始化原理及plus is not defined,mui is not defined 错误汇总

             服务器如何接收前台传送的json数据,链接:Java后台如何接收并处理前台传过来的json格式的数组参数 

             ajax请求如何处理服务器返回的json数据,链接:jquery的ajax异步请求接收返回json数据

2.angular js使用http请求:

<html ng-app="mine_main">
<body class="mui-fullscreen" ng-controller="LSControllerForMineMain">
ng-model---变量名

ng-app---整体

ng-controller---控制器

ng-init---初始化

var app = angular.module('mine_main', []);        app.controller('LSControllerForMineMain', function($scope, $location, $http) {            //alert(JSON.parse(localStorage.getItem("userInfo")).realname);            $scope.realname = localStorage.getItem("realname");            $scope.data = null;            var wgtVer = null;            $http({                method: 'POST',                url: LSHelper.getInstance().getValue('baseurl') + '/respection_problems/getRoleName.action',                header: {                    'Content-Type': 'application/x-www-form-urlencoded',                    'Cookie': LSHelper.getInstance().getValue('sessionid')                }            }).then(function successCallback(response) {                //alert(JSON.stringify(response));                if(response.data.success == 1) {                    $scope.roleName = response.data.data;                } else {                    alert("error,code:" + response.data.success);                }            }, function errorCallback(response) {                alert("error");            });                     $scope.updateVersion = function() {                // alert("更新版本");                // 获取本地应用资源版本号                plus.runtime.getProperty(plus.runtime.appid, function(inf) {                    wgtVer = inf.version;                    //                    alert("当前应用版本:" + wgtVer);                });                if(wgtVer != null) {                    var _config = {                        url: 'http://60.30.254.100:8035/'//                        url: 'http://192.168.1.108:8080/'                    }                    var checkUrl = _config.url + "version/compareVersion.action"                    plus.nativeUI.showWaiting("检测更新...");                    $http({                        method: 'POST',                        url: checkUrl,                        header: {                            'Content-Type': 'application/x-www-form-urlencoded'                        },                        data: {                            "appversion": wgtVer,                            "id": "3"                        }                    }).then(function successCallback(response) {                        plus.nativeUI.closeWaiting();                        //                        alert(JSON.stringify(response));                        if(response.data.success == 1) {                            alert("当前已是最新版本");                        } else if(response.data.success == 0) {                            var btn = ["确定", "取消"];                            mui.confirm('检测到有新版本可更新,确定更新吗?', '', btn, function(e) {                                if(e.index == 0) {                                    //                                    alert(response.data.url);                                    downWgt(response.data.url);                                }                            });                        }                    }, function errorCallback(response) {                        alert("网络错误!");                    });                }                sessionStorage.setItem('upgrade', '1');            }        });
});

java后台代码:

@RequestMapping("/compareVersion")public void toList(HttpServletRequest req, HttpServletResponse res)throws UnsupportedEncodingException {req.setCharacterEncoding("utf-8");System.out.println("AAAAAAAAAAAA");String contentStr = getJson(req);System.out.println("contentStr==="+contentStr);JSONObject json = JSONObject.fromObject(contentStr);System.out.println("解析后的json数据===" + json.toString());String appversion = json.getString("appversion");String id = json.getString("id");JSONObject jsonObject = new JSONObject();if (id != null && appversion != null) {Version version = dao.getVersionById(Integer.parseInt(id));if (Float.parseFloat(appversion) < Float.parseFloat(version.getV_num())) {jsonObject.put("success", "0");jsonObject.put("url", version.getUrl());} else {jsonObject.put("success", "1");}} else {jsonObject.put("success", "-1");}returnResponse(res, jsonObject.toString());}private String getJson(HttpServletRequest request) {        InputStream is = null;        String contentStr = "";        try {            is = request.getInputStream();            contentStr = IOUtils.toString(is, "utf-8");            System.out.println("得到的contentStr数据===" + contentStr);        } catch (IOException e) {            e.printStackTrace();        }        return contentStr;    }









原创粉丝点击