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; }
阅读全文
0 0
- ajax请求和angular js中http请求
- jquery ajax 和 angular 跨域请求
- AJAX请求和普通HTTP请求区别
- Angular----$http请求数据
- $http——ajax跨域请求的angular方式
- angular的$http的ajax的请求操作
- angular 取消或终止$http的ajax请求
- JS中使用HTTP请求
- angular.js学习之三ajax请求得不到数据
- angular用$http请求数据
- angular的http请求方式
- angular中的http请求封装
- AJAX中出现两次请求,OPTIONS请求和GET请求
- cocos2d-js 中封装HTML5和Native的Http请求
- Angular和jQuery的ajax请求的区别
- angular 路由请求js文件
- angular $q封装$http get和post请求
- ajax Http请求方式
- 909422229_DOM4J读取XML文件与解析StringXML
- 最短路径之Bellman_Ford
- 字符串:HDU5371-Hotaru's problem(manacher 的应用)
- php 定时服务
- JavaScript的六种继承方式
- ajax请求和angular js中http请求
- 区块链赚钱攻略:最新干货 区块链人士必读!
- 主线程统计子线程执行时间---countDownLatch
- C语言关键字const用法
- bash:command not found解决方法
- JAVA中去掉空格
- 高性能WEB服务器——之Jetty (N)I/O组件
- JAVA List 迭代遍历案例
- JdbcDaoSupport配合@Repository无法注入DataSource