Unable to redirect with Asp response.redirect

I have a Login popup form where I use an ajax post request to Login.asp script in order to prevent the page from going to the POST URL after submission.

<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 display an Error message in the popup form. If login is successful, I need to close the popup form and redirect.

The issue is that I am getting redirected page sources inside my popup form.

I have tried doing it this way:

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

While the popup is closed, the page is not being redirected properly.

I also attempted the following:

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

Even though the popup is closed, the page is still not being redirected as expected.

How can I resolve this issue? How do I successfully close the popup and redirect the page normally?

Answer №1

When an AJAX request is made, server side redirection does not impact the browser itself; only the AJAX request object will receive the redirected page source in the response.

To perform a client-side redirection, you must use response.write to output the URL.

Here is an 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;
    }
}

Similar questions

If you have not found the answer to your question or you are interested in this topic, then look at other similar questions below or use the search

Is there a way to create a dynamic associative array using jquery?

I am looking to create an array structured like the following:- arr[0][from] = value arr[0][to] = value arr[1][from] = value arr[1][to] = value . . And so forth. I have input array html elements for the from & to fields. <input type="text" name ...

React Alert: "Unable to modify while in the middle of a state transition"

Currently, I am working on developing an application that involves utilizing a toastr component. However, I am encountering an error when attempting to dispatch a redux action within this particular component. The error message displayed in the console is ...

Tabcontainer in ajax not displaying tabs correctly in Internet Explorer 8

Currently, I am utilizing an AJAX tab container inside an update panel (along with a ToolScriptManager placed in the master pages) to display two tab controls. Interestingly, the tabs are rendering without any issues in Firefox, however, they are throwing ...

In JavaScript, a "switch statement" retrieves a test value from a "input type text" by accessing the value using getElementByName.value

Attempting to test 7 possible values for the variable 'a' by passing it to a function from user input in seven 'input type text' boxes, then checking each value individually with clicks on 7 'input type image' elements. Howeve ...

Say goodbye to my HTML5 creations

I am currently working on an HTML5 grid project that involves implementing a rectangle select tool for use within the grid. Everything is going smoothly, except for the fact that when I attempt to use the rectangular select tool, the grid disappears from t ...

Bring JavaScript Function into Vue's index.html File

Recently in my code files, I noticed the following: function example() { console.log("testing"); } In another file, I have something like this: <head> <script src="../src/example.js" type="text/babel"></sc ...

Angular often uses the JavaScript pattern for development

After completing an AngularJS tutorial on http://www.tutorialspoint.com/angularjs/angularjs_services.htm, I found myself puzzled by the method used in the CalcService service. It seemed unclear whether Angular was using revealing prototype or a different ...

Enabling automatic scaling during the rendering of an .stl file using three.js

I'm in the process of developing a server/webpage with the following functionality: Users can upload an .stl file to be 3D-printed The server executes a mesh repair script on the uploaded .stl file The corrected .stl file is displayed in the user&ap ...

Is it possible to implement an automatic clicking function on a bootstrap navigation bar similar to a carousel?

I am working on a website with a bootstrap navigation bar similar to the one shown in this image : bootstrap nav <ul class="nav nav-tabs" id="tab_home" role="tablist"> <li class="nav-item" role="pres ...

The issue arises when using an Angular directive with an input type set as "number"

When the input type is text, the code below works perfectly fine. However, it fails to function when the input type is changed to number. <div ng-app="myApp" ng-controller="myCtrl as model"> <input type="text" ng-model="cero" ng-decimal > ...

Having trouble drawing over buttons in HTML5 Canvas?

window.addEventListener("load", () => { const canvas = document.querySelector("#canvas"); const ctx = canvas.getContext("2d"); const color = document.querySelector("#color"); const strokeWeight = document.querySelector("#strokeWeight"); ...

Has Jade stopped allowing inline variables unless enclosed by JavaScript markers?

Back when I was using Jade version 0.34.1 (prior to the release of version 1.0.0), I had the ability to incorporate inline variables like this: test = 'fun' p #{test} This would typically result in: <p>fun</p> However, the output ...

What's the advantage in using 2 functions instead of just utilizing one?

When I'm asking for help with my code, I make sure to include all of it in case there's a connection between the different functions. Recently, I received assistance to get one of my functions working properly. Specifically, I'm looking at t ...

Is it possible to extract the body from the post request using req.body.item?

After working with Express, I learned how to extract body data from a post request. Most examples showed that using req.body.item should retrieve the desired value for tasks like inserting into a table. However, in my case, I found that I couldn't ac ...

Issues encountered with JSON formatting following jQuery ajax request

When my nodejs app receives data from a cordova app through a jQuery ajax call, the format is different. It looks like this: { "network[msisdn]": "+254738XXXXXX", "network[country]": "ke", "network[roaming]": "false", "network[simSt ...

Utilize only certain JSON properties within JavaScript

I have access to an array of JSON objects. [ { Id: "1", StartNo: "1", ChipNo: "0", CategoryId: "0", Wave: "0", Club: "", FirstName: "Lotta", LastName: "Svenström", FullName: "Lotta Svenström", ZipCode: "24231" }, {...} ] My goal is to create a new data ...

Identifying a specific item within an array

Can someone guide me on how to manipulate a specific object within an array of objects? For example: var myArray = [ {id: 1}, {id: 2}, {id: 3}, {id: 4} ] // Initial state with the 'number' set as an array state = { number: [] } // A functio ...

The undead browser refuses to display a window upon attempting to open using the open function

tools first attempt Darwin 14.3.0 Darwin Kernel Version 14.3.0 io.js v1.8.1 zombie Version 4.0.7 released on April 10, 2015 second attempt Linux ubuntuG5 3.13.0-48-powerpc64-smp node.js v0.10.38 zombie Version 3.1.0 released on March 15, 2015 comman ...

Dispatching current to the default case of the switch statement

Hello everyone, I wanted to share a snippet from my JS file: function passDataToPHP() { endpoint = 'myfile.php'; parameters = ''; parameters += 'action=passData'; parameters += '&myfield='+someV ...

Is it possible to selectively process assets based on their type using Gulp and Useref?

Is it possible to selectively process css and js assets using gulp-useref based on the build type specified in the html tags? <!-- build:<type>(alternate search path) <path> --> ... HTML Markup, list of script / link tags. <!-- endbui ...