Javascript 注册后重定向[react,react router,alt]
我正在使用React创建我的第一个应用程序,遇到了一个我确信很容易解决的问题,但我不知道如何解决。我无法让用户在注册后看到新的“成功”页面。 我认为重定向应该处于状态,但我似乎找不到正确的呼叫。这就是我到目前为止所做的: SignUpForm.js SignUpFormStore.js Routes.jsJavascript 注册后重定向[react,react router,alt],javascript,reactjs,react-router,Javascript,Reactjs,React Router,我正在使用React创建我的第一个应用程序,遇到了一个我确信很容易解决的问题,但我不知道如何解决。我无法让用户在注册后看到新的“成功”页面。 我认为重定向应该处于状态,但我似乎找不到正确的呼叫。这就是我到目前为止所做的: SignUpForm.js SignUpFormStore.js Routes.js 从“React”导入React; 从“反应路由器”导入{Route}; 从“./components/App”导入应用程序; 从“./components/Home”导入Home; 从“./c
从“React”导入React;
从“反应路由器”导入{Route};
从“./components/App”导入应用程序;
从“./components/Home”导入Home;
从“./components/LoginForm”导入LoginForm;
从“./components/SignUpForm”导入SignUpForm;
从“/components/Success”导入成功;
导出默认值(
);
实际上,您可以在ajax调用中执行重定向:
$.ajax({
type: 'POST',
url: 'https://app.herokuapp.com/users',
crossDomain: true,
dataType: 'json',
data: data
})
.done((data) => {
window.location.href = "url redirect to success page";
this.actions.addUserSuccess(data.message);
})
.fail((jqXhr) => {
this.actions.addUserFail(jqXhr.responseJSON.error.message);
});
$.ajax({
键入:“POST”,
网址:'https://app.herokuapp.com/users',
跨域:是的,
数据类型:“json”,
数据:数据
})
.完成((数据)=>{
window.location.href=“url重定向到成功页面”;
this.actions.addUserSuccess(data.message);
})
.失败((jqXhr)=>{
this.actions.addUserFail(jqXhr.responseJSON.error.message);
});
您也可以使用React路由器执行此操作:
this.history.pushState(null,“/success”)代码>太棒了!当我对您的代码做了一个小的更改时,这起作用了window.location.href=“/success”代码>非常感谢!!随时我继续在我的回答中更新了这一点。你可能还想检查一下:我不同意这个解决方案,这是本机js解决方案,但如何使用react router?只是历史记录中的一个注释-对于我们这些不幸需要支持IE9的人来说-pushState不受该浏览器的支持。你为什么选择Alt,而不是雷杜?我们也在考虑使用Alt。
import alt from '../alt';
import {assign} from 'underscore';
class SignUpFormActions {
constructor() {
this.generateActions(
'addUserSuccess',
'addUserFail',
...
);
}
addUser(first_name, last_name, email, phone, password, passwordConfirmation, tos){
var data = {
first_name: first_name,
last_name: last_name,
email: email,
phone_number: phone,
password: password,
password_confirmation: passwordConfirmation,
tos: tos
};
$.ajax({
type: 'POST',
url: 'https://app.herokuapp.com/users',
crossDomain: true,
dataType: 'json',
data: data
})
.done((data) => {
this.actions.addUserSuccess(data.message);
})
.fail((jqXhr) => {
this.actions.addUserFail(jqXhr.responseJSON.error.message);
});
}
import alt from '../alt';
import SignUpFormActions from '../actions/SignUpFormActions';
class SignUpFormStore {
constructor() {
this.bindActions(SignUpFormActions);
this.first_name= "";
...
}
onAddUserSuccess(successMessage) {
console.log('add user success!');
//I think the user should be re-directed here
//I have tried these two options but they don't work
//Route.get().transitionTo('/success');
//transition.redirect('success');
}
onAddUserFail(errorMessage){
console.log('add user error! ' + errorMessage);
this.helpBlock = errorMessage;
}
...
}
export default alt.createStore(SignUpFormStore);
import React from 'react';
import {Route} from 'react-router';
import App from './components/App';
import Home from './components/Home';
import LoginForm from './components/LoginForm';
import SignUpForm from './components/SignUpForm';
import Success from './components/Success';
export default (
<Route handler={App}>
<Route path='/' handler={Home} />
<Route path='/login' handler={LoginForm} />
<Route path='/signup' handler={SignUpForm} />
<Route path='/success' handler={Success} />
</Route>
);
$.ajax({
type: 'POST',
url: 'https://app.herokuapp.com/users',
crossDomain: true,
dataType: 'json',
data: data
})
.done((data) => {
window.location.href = "url redirect to success page";
this.actions.addUserSuccess(data.message);
})
.fail((jqXhr) => {
this.actions.addUserFail(jqXhr.responseJSON.error.message);
});