如何解决从 xmlhttprequest 后调用转移到不同的 url
我有一个像这样的 xmlhttprequest 方法,它成功地从谷歌登录中获取经过身份验证的数据,然后我需要将数据发布到登录视图。服务端使用python和django。
<form name="Login" method="POST" id="loginForm" action="/login">
function onSignIn(googleUser) {
// Useful data for your client-side scripts:
var profile = googleUser.getBasicProfile();
console.log("ID: " + profile.getId());
console.log('Full Name: ' + profile.getName());
console.log('Given Name: ' + profile.getGivenName());
console.log('Family Name: ' + profile.getFamilyName());
console.log("Image URL: " + profile.getImageUrl());
console.log("Email: " + profile.getEmail());
var id_token = googleUser.getAuthResponse().id_token;
console.log("ID Token: " + id_token);
alert("Token*"+id_token); //this gets executed
var xhr = new XMLHttpRequest();
xhttp.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
}
};
xhr.open('POST','/login');
xhr.setRequestHeader("X-CSRFToken",'{{ csrf_token }}');
xhr.setRequestHeader('Content-Type','application/x-www-form-urlencoded');
xhr.onload = function() {
console.log('Signed in as: ' + xhr.responseText);
alert("*******Token*"+id_token)
try
{
xhr.send('idtoken=' + id_token);
}
catch (e)
{
alert("*"+e.toString)
console.log('send() error: ' + e.toString());
return false;
}
views.py
在视图中我定义了一个登录方法()
def login(request):
if request.method=='POST':
#process the request and send the data to another page with some additonal parameters
role=user_role
email=user_email
fullname=user_name
return render(request,'ba/dashboard.html',{'role':role,'email':email,'fullname':fullname})
发布请求完成后,页面不会转移到dashboard.html 页面,它只停留在同一个登录页面上。我希望它使用给定的参数传输到dashboard.html页面。
这是post完成后没有GET请求通过的控制台输出
[09/Feb/2021 15:53:05] "POST /login HTTP/1.1" 200 4742
谁能告诉我如何实现这一目标?谢谢
解决方法
这是为了转移到不同的网址我必须添加这一行
xhr.onreadystatechange = function() {
if (this.readyState == 4 && this.status == 200) {
alert(this.responseText);
**window.location.href="/home"** //I had hoped is there any other way to achieve without using this
}
为了传递参数,我从 post 方法中执行了此操作并进入了 onreadystatechange,如上所示:
return HttpResponse(
json.dumps({'role':user_role,'email':user_email,'fullname':user_name})
)
版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。