ユーザーのログイン後にヘルプが必要です。データに結果がある場合はページをリダイレクトしようとしましたが、間違った電子メールまたはパスワードでログインすると、エラーを警告するのではなくページがリダイレクトされます。私はAPIのトークンを使っています。リダイレクトページES6フェッチでのログイン後
function loginUser(){
fetch('http://example_website.com/api/login', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
email: document.getElementById("email").value,
password: document.getElementById("password").value
})
})
.then(data => data.json())
.then(data => {
if(data){
redirect: window.location.replace("../Sample/home.html")
} else{
alert("Invalid Email or Password");
}
})
.catch((err) => {
console.error(err);
})
}
function registerUser(){
fetch('http://example_website.com/api/register', {
method: 'POST',
headers: {'Content-Type': 'application/json'},
body: JSON.stringify({
given_name: document.getElementById("given_name").value,
last_name: document.getElementById("last_name").value,
email: document.getElementById("email").value,
password: document.getElementById("password").value,
password_confirmation: document.getElementById("confirm_password").value
})
})
.then(data => data.json())
.then(data => { console.log(data);
})
.catch((err) => {
alert ("Error!");
console.error(err);
})
}
有効API応答:
無効なAPIのresopnse:
あなたのコードをデバッグすると、この行の 'data'変数の中で、' .then(data => data.json()) 'のどちらの場合に成功しますか? – randomguy04
@ randomguy04。私はAPIからの応答を得た。ユーザーが正しくログインした場合と同様に、応答はログインに成功し、トークンを持ちます。そうでない場合は、検証に失敗したと表示されます。私は、console.log(data)を使用して、ブラウザのコンソールに結果を表示しようとしました。それはすべてログインapiから来る – Joseph
あなたの質問を編集し、検証が失敗したときにAPIの応答を投稿できますか? – randomguy04