Warning: file_get_contents(/data/phpspider/zhask/data//catemap/5/spring-mvc/2.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery ajax登录重定向_Jquery_Spring Mvc_Login_Response - Fatal编程技术网

Jquery ajax登录重定向

Jquery ajax登录重定向,jquery,spring-mvc,login,response,Jquery,Spring Mvc,Login,Response,我有一个简单的弹出式登录,它将状态返回到div(如果pass不正确)或重定向到主页 以下是处理请求的代码: $.ajax({ type: "POST", url: "login.html", cache: false, data: formData success: function(data){ var $response=$(data); $('#response').text($response.find('

我有一个简单的弹出式登录,它将状态返回到div(如果pass不正确)或重定向到主页

以下是处理请求的代码:

$.ajax({
     type: "POST",
     url: "login.html",
     cache: false,
     data: formData
     success: function(data){
        var $response=$(data);
        $('#response').text($response.find('#response').text());
        if($('#response').text().length<=0)
        {
            window.location="main.html";
        }
     }
 });
$.ajax({
类型:“POST”,
url:“login.html”,
cache:false,
数据:formData
成功:功能(数据){
变量$response=$(数据);
$('#response').text($response.find('#response').text());

如果($('#response').text().length看到当您从Spring重定向时,页面将如何重定向..因为您正在进行ajax提交..当您从Spring重定向并返回主页时,您可以在ajax调用的变量数据中看到主页html。 规则很简单..当您使用ajax..浏览器正常提交不会干扰..在理想情况下,当存在正常提交时,服务器将以302的形式发送HTTP代码,并且您的页面将重定向到新位置(意味着新页面将打开)

您可以尝试以下方法:

$.ajax({
     type: "POST",
     url: "login.html",
     cache: false,
     data: formData
     success: function(data){
       document.open();
       document.write(data);//home page from server after authentication..dont forget to add redirect logic at spring
       document.close();
     }
 });
我希望您没有使用Spring security进行身份验证。因此,您可能需要编写许多自己的筛选方法。
另外,尝试使用授权的正常提交方法来代替ajax。因为在ajax中,您可能需要使用自己的会话id设置浏览器cookie。这在正常的身份验证过程中会很容易处理。谢谢

@extra90同意您的意见。但这个答案是实现目标的最小更改。最佳做法是使用正常提交身份验证的声明。如果您同意回答,请接受。谢谢