简体   繁体   中英

Asp response.redirect not redirecting

I have a Login popup form where I send ajax post request to Login.asp script to prevent go to POST URL after submit.

<script>

$(function() {
    $('#contactForm').submit(function(e){
        e.preventDefault();

        $.ajax({
            url:'/ASPLogin/Login.asp',
            type:'POST',
            data:$('#contactForm').serialize(),
            dataType:'text',
            success: function(response)
            {
            var temp = new Array();
            temp = response.split("|");

            if (temp[0] == 'something') {

              }
              else {

                  $("#response").html(response);
             }
            }
        });

    });
});

</script>

in Login.asp

If OK Then
 response.redirect("/index.asp?token=" & str)
Else
 response.write("error")
End If

So when login is wrong I put Error message into popup form, if login is OK then I need to close popup form and redirect.

But I get redirected page sources inside my popup form.

I tried to do like that

If OK Then
 response.write("<script>$('#closeBut').click();</script>")
 response.redirect("/index.asp?token=" & str)
Else
 response.write("error")
End If

Popup is closed but page is no redirected

I tried to do like that

If OK Then
 response.write("<script>$('#closeBut').click();</script>")
 Response.Write("<script>window.location.href = " & "'/index.asp?token=" & str & "';</script>")
Else
 response.write("error")
End If

Popup is closed but page is no redirected

How to fix that? How to close popup and redirect page normally?

The server side redirection won't affect the browser itself when the request is made from ajax, only the ajax request object will see the redirected page source in the response.

You need to response.write the url so that you can make a client-side redirection

For example:

If OK Then
 response.write("/index.asp?token=" & str)
Else
 response.write("error")
End If

then in the ajax success event:

success: function(response)
{

    if (response != 'error') {
        location.href = response;
    }
}

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM