Angular.js v1.0.6
制作$ http.post并收到非200 resposne(在这种情况下为401)
$http.post('http://localhost:3030/auth/login', {
username: 'username',
password: 'password'
})
.success(function(data) {
// Gets called on a 200 response, but not on a 401
console.log('success');
})
.error(function(err) {
// Never gets called & dies with error described below.
console.log('error');
});
Run Code Online (Sandbox Code Playgroud)
Angular会抛出以下错误:
TypeError: Cannot read property 'data' of undefined
at http://localhost:9000/components/angular/angular.js:8891:22
at wrappedCallback (http://localhost:9000/components/angular/angular.js:6797:59)
at http://localhost:9000/components/angular/angular.js:6834:26
at Object.Scope.$eval (http://localhost:9000/components/angular/angular.js:8011:28)
at Object.Scope.$digest (http://localhost:9000/components/angular/angular.js:7876:25)
at Object.Scope.$apply (http://localhost:9000/components/angular/angular.js:8097:24)
at done (http://localhost:9000/components/angular/angular.js:9111:20)
at completeRequest (http://localhost:9000/components/angular/angular.js:9274:7)
at XMLHttpRequest.xhr.onreadystatechange (http://localhost:9000/components/angular/angular.js:9244:11)
Run Code Online (Sandbox Code Playgroud)
并且从不调用.success()回调或错误.error()回复使得无法处理响应. …
javascript authentication http http-status-code-401 angularjs
更新
感谢@Dominic Tobias和@gabdallah发现我令人尴尬的错误.
当然是正确的答案;
所以试试看
action.payload.
关于switch语句和action对象的其他注释我们指的是我在我的例子中做出的错误,我已经纠正过了.
想象一下,我结合了以下两个减速器;
import { combineReducers } from 'redux'
import { routerStateReducer } from 'redux-router'
import entries from './entries'
export default combineReducers({
router: routerStateReducer,
entries
})
Run Code Online (Sandbox Code Playgroud)
在这种情况下,我想基于全局状态的另一部分来改变条目状态; redux-router提供的路由器状态,以便例如实现分页.
我怎么能这样做?
// entries.js
import { ROUTER_DID_CHANGE } from 'redux-router/lib/constants'
const initialState = {}
function entries (state = initialState, action) {
switch (action.type) {
case ROUTER_DID_CHANGE:
// How can I access `state.router` here in order to do something like this;
if (routerState.location.pathname === '/entries') {
return { …Run Code Online (Sandbox Code Playgroud) 想象一下,我已经定义了以下自定义验证器功能:
isUnique: function () { // This works as expected
throw new Error({error:[{message:'Email address already in use!'}]});
}
Run Code Online (Sandbox Code Playgroud)
但是,当我尝试查询数据库时遇到问题:
isUnique: function (email) { // This doesn't work
var User = seqeulize.import('/path/to/user/model');
User.find({where:{email: email}})
.success(function () { // This gets called
throw new Error({error:[{message:'Email address already in use!'}]}); // But this isn't triggering a validation error.
});
}
Run Code Online (Sandbox Code Playgroud)
如何在自定义验证器中查询ORM并根据ORM的响应触发验证错误?
javascript ×3
angularjs ×1
http ×1
node.js ×1
orm ×1
react-router ×1
reactjs ×1
redux ×1
sequelize.js ×1