因此,对于试图找出这个问题的人来说,我前一段时间发现了它。
首先,您需要在您的React组件中有Router。
var withRouter = require('react-router').withRouter;
var SomeComponent = withRouter(
React.createClass({
PropTypes: {
},
.........
接下来,我们建立我们的服务能够调用Router:
var router = null;
var setRouter = function (routerToSet) {
router = routerToSet;
}
//..... other stuff
module.exports: {
//.....
setRouter: setRouter
}
现在,我们可以称之为 - 上Ajax错误 - 路由器重定向
function handleErrorResponse(error, router) {
if(error.status == statusCodes.UNAUTHORIZED) {
if(router) {
router.replace(unauthorizedPath);
}
else {
toastr.error("Router not defined", "CRITICAL ERROR");
throw new Exception("CRITICAL ERROR: Router not defined");
}
}
else {
return error;
}
}
function handleError(error) {
return handleErrorResponse(error, router);
}
var myAjaxCallToHandle= function() {
return $.ajax({
url: routes.where_ever,
data: JSON.stringify(data),
method: "POST",
contentType: "application/json"
})
.error(handleError);
}
的只剩下要做的事情是,在组件中,加上Router
//.........
getInitialState: function() {
unitPlantService.setRouter(this.props.router);
}
//.........