1. angularJS的$http.post請求,SPRingMVC后臺接收不到參數(shù)值的解決方案
問題一般為:400 Required String parameter 'rPassWord' is not present 或其他400錯誤
解決方法 一:修改源碼http://my.oschina.net/buwei/blog/191640 (可以詳細(xì)了解請求接收不到的原因)
解決方法 二:感覺修改源碼總是不好滴,可以采用這個方法,修改提交參數(shù)config的headers和transformRequest
(1) 創(chuàng)建一個全局的transformRequest function
var app = angular.module('myApp');app.config(function ($httpProvider) { $httpProvider.defaults.transformRequest = function(data){ if (data === undefined) { return data; } return $.param(data); }});然后為每一個方法或者創(chuàng)建一個全局的Content-Type header
$httpProvider.defaults.headers.post['Content-Type'] = 'application/x-www-form-urlencoded; charset=UTF-8';(2) 為每一個需要的方法創(chuàng)建局部的transformRequest
var transform = function(data){ return $.param(data); } $http.post("/foo/bar", requestData, { headers: { 'Content-Type': 'application/x-www-form-urlencoded; charset=UTF-8'}, transformRequest: transform }).success(function(responseData) { //do stuff with response });參考網(wǎng)址AngularJS - Any way for $http.post to send request parameters instead of JSON?
2. scope.$apply的使用
問題一般為:數(shù)據(jù)雙向綁定失效,就是明明在controller里面給$scope.×××賦值了,在頁面上xxx愣是顯示不了,但是點擊一下輸入框或是form表單的提交按鈕,xxx數(shù)據(jù)信息就顯示了,是不是好坑啊。
解決方法 : 添加 $scope.$apply()
例如
$scope.$apply(function(){ $scope.xxx = “你賦的值”;});原因
一般情況下是不需要我們手動添加這一句代碼的,因為angularJS本身在需要的時候調(diào)用,以達(dá)到我們所看到的數(shù)據(jù)雙向綁定的效果。
但是你若是引用一個外部插件或者其他,在回調(diào)函數(shù)里創(chuàng)建或更新$scope.xxx的數(shù)據(jù),因為外部插件本身已經(jīng)脫離了angularJS的作用域,所以數(shù)據(jù)雙向綁定在這里沒有效果,只能手動添加$scope.$apply()來通知頁面獲取數(shù)據(jù)。
3. 因為是新手,一步一步的來
新聞熱點
疑難解答