javascript - 如何在成功ajax响应时重定向到其他html文件

标签 javascript jquery ajax

我需要你的帮助,了解如何在 javascript 中成功 ajax 响应时重定向到其他 html 文件。

下面是我在 jsonvariable 中获得成功的 ajax 响应的代码。

我试过使用 window.location 但它不起作用。有没有其他方法可以做到这一点,请让我知道

function myFunction() {

    $.ajax({
			url: '/ValidateOTP',
			type: 'POST',
			data:JSON.stringify( $('#OTP').val()),
			contentType: 'application/json;charset=UTF-8',
			success: function(response){
			    jsonvariable=response['success'].toString();
			    if(jsonvariable=='true')
			          alert('done');

			    else if(jsonvariable=='false')
			        document.getElementById("div2").innerHTML="OTP didn't match !! Please click the GET OTP button to re-generate OTP";
                    document.getElementById("div2").style.color="Red";

            },
			error: function(response){
			        alert(response)

			}
		});
};

最佳答案

你可以 window.location.href = ' http://www.google.com ';或者 window.location = ' http://google.com ';

函数 myFunction() {

$.ajax({
        url: '/ValidateOTP',
        type: 'POST',
        data:JSON.stringify( $('#OTP').val()),
        contentType: 'application/json;charset=UTF-8',
        success: function(response){
            jsonvariable=response['success'].toString();
            if(jsonvariable=='true'){
                window.location.href = 'http://www.google.com';
            }

            else if(jsonvariable=='false')
                document.getElementById("div2").innerHTML="OTP didn't match !! Please click the GET OTP button to re-generate OTP";
                document.getElementById("div2").style.color="Red";

        },
        error: function(response){
                alert(response)

        }
    });

};

关于javascript - 如何在成功ajax响应时重定向到其他html文件,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/58463091/

相关文章:

javascript - 使固定的 div 保持水平固定

asp.net-mvc - ASP.NET MVC 应用程序中 MS Ajax 与 jQuery 的优缺点?

jquery - Ajax 更新了用户界面和后退按钮

javascript - PHP Ajax 从表中删除一条记录

javascript - 保存时 extjs ServerProxy 错误

javascript - 5 秒后显示 JS 警报

javascript - 为什么 jQuery.post 最终出现在 PHP 的 $_POST 中,而我的普通 JS 帖子却没有?

javascript - 如何存储 'this' 元素供以后使用

javascript - 如果单选框是唯一显示的,则自动选中它

javascript - react 路由: URL is changing but page is not loading