AngularJS allows for submitting form data to a new window by utilizing the form

At the moment, I have a JavaScript function that sends a form POST request and opens it in a new window. Now, I want to convert this into AngularJS.

Here's the current function. The three parameters passed in determine the post URL and some data values on the POST request.

var login = function(postUrl, samlResponse, samlDomain){
    var form = $('<form></form>');
    form.attr('method', 'post');
    form.attr('action', postUrl);
    form.attr('target', '_blank');
    var field = $('<input></input>');
    field.attr('type', 'hidden');
    field.attr('name', samlResponse);
    field.attr('text-wrap','none');
    field.attr('value', 'response-value');
    form.append(field);
    var field2 = $('<input></input>');
    field2.attr('type', 'hidden');
    field2.attr('name', 'RelayState');
    field2.attr('value', samlDomain);
    form.append(field2);
    $(document.body).append(form);
    form.submit();
}

My attempt to achieve the same functionality using AngularJS and $http is shown below:

$scope.login = function(postUrl, samlResponse, samlDomain) {
    var tabWindowId = window.open('about:blank', '_blank');
    var data = {
        SAMLResponse: samlResponse,
        RelayState: samlDomain
    }
    $http.post(postUrl, data).then(function (response) {
        tabWindowId.location.href = response.headers('Location');
    });
}

I'm encountering an error related to not being able to access the response due to CORS policy restrictions. However, I don't actually need to access the response, just open it in a new window.

XMLHttpRequest cannot load {{postUrl}}. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:7631' is therefore not allowed access. 

What would be the best way to achieve my goal using AngularJS?

Another approach I'm considering is adding the form to the HTML template but hiding it with ng-hidden. Then, somehow triggering the form submit from within my AngularJS controller, which feels counterintuitive.

Answer №1

After much consideration, I decided to implement the login function directly into my AngularJS controller. While I have reservations about this approach, as it involves using jquery $(document.body).append(form), it served as a speedy resolution. However, I am open to exploring alternative solutions and would appreciate any additional insights.

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

What is the most efficient way to retrieve key pair values of separate elements in an array with just one API request?

The API I am working with returns an array of elements, each containing multiple key-value pairs. An example of a similar API response can be seen here: , where an array of poems is returned. [ { "title": "...." "content": "..." "url" : "..." } ...

Looking for guidance on integrating Forms with VueX and script setup? Are you utilizing v-model in your implementation?

UPDATE: I'm contemplating whether or not to abandon VueX entirely due to its outdated status, with Pinia being the preferred choice nowadays. Can someone confirm this? https://stackblitz.com/edit/vue-script-setup-with-vuex-hmrk5d?file=src/store.ts ...

Performing an automated check on user messages every 60 seconds using JQuery and Ajax

I am in the process of developing a website with notification features, and I am looking to implement a script that will check for new messages every 60 seconds. The goal is to pass the user id through the script to trigger an alert (currently using a ba ...

Switch Bootstrap Tab

I have successfully implemented a bootstrap tab on my webpage and it is functioning as intended. Now, I am interested in adding an additional feature to the tabs. My question is, is it possible to toggle the content area if the same tab is clicked again? ...

Improper menu alignment following a MenuItem hover event within Material-UI

When a MenuItem is hovered over, the position of the Menu container should remain unchanged. [x] The issue persists in the most recent release. However, downgrading MUI to v4.5.0 results in the expected behavior. Current Behavior ...

Verifying the activation status of a button within a Chrome extension

I have been working on a chrome plugin that continuously checks the status of a button to see if it is enabled. If it is, the plugin clicks on the button. I initially used an infinite for loop for this task, but realized that it was causing the browser to ...

Working with Vue.js: Utilizing computed properties to iterate through a JSON data structure and generate a new

This is a computed property in Vue.js: shopCart() { var scart = [], group, node; scart.push({ payMethod: 0, transactionReference: "", RoomNumber: 0, addressId: 0, deliveryMinutes ...

Exploring the functionality of AngularJS Ui-Router in conjunction with ASP.Net MVC's RouteConfig - a deep dive into

I'm currently studying this informative article and I'm finding it tricky to wrap my head around how Angular's UI Router works in conjunction with ASP.Net routing. Could someone simplify and explain the entire process starting from when a U ...

The CSRF token in Laravel Blade seems to be unreachable by Vue

Having a blade with the following code snippet. <meta name="csrf-token" content="{{ csrf_token() }}"> <covid-form> </covid-form> Inside the covid-form component, there is a form structure like this: <form @submit.prevent="send"&g ...

Is it possible to continuously increase the value of a CSS property with each click?

I am trying to implement a feature where the value of an element decreases each time a different element is clicked. Currently, I have the following code: $("#trigger_heritage").click(function () { $(".heritage_content ul").css("margin-left", + -880); ...

Is it possible to achieve a fade effect in material-ui that truly hides the component instead of just disabling its visibility? If so, how can this be accomplished?

Currently, I am utilizing the component provided by material-ui, which can be found at material-ui. <Fade in={!randomizeFlag}> <Grid> <FormControlLabel control={<Switch onChange={this.handleStartValueFlag} & ...

Google Maps does not support markers appearing on the map

I have created a basic web application that displays markers from a MySQL database on Google Maps using a table called markers_titik. In order to process this data, I have written a simple PHP script named map_process.php. Here is the code: <?php //PH ...

Creating a carousel in AngularJS using ng-repeat without resorting to jQuery

Looking for a solution to display elements on my page with ng-repeat in a carousel-like fashion. Each element should have a photo, short description, and reveal a long description on click. It's important that it's mobile-friendly so users can sw ...

Vue.js is unable to dispatch an event to the $root element

I'm having trouble sending an event to the root component. I want to emit the event when the user presses enter, and have the root component receive it and execute a function that will add the message to an array. JavaScript: Vue.component('inp ...

Steps for interacting with a button of the <input> tag in Selenium using Python

As I attempt to complete a form submission, I encounter an issue where clicking the submit button does not produce any action. It seems that the problem lies with the button being tagged as <input>: <input type="submit" name="submit ...

Error in template loading

Currently, I am going through the Angular.js tutorial to learn how to incorporate some of its advanced features into my existing applications. Everything has been smooth sailing until I reached the (angular route) section. Unfortunately, I've hit a ro ...

Is it possible to access dl, dt, or dd tags based on their roles within the testing library?

Is there a way to use the testing library to specifically target the dd tag based on its role? <dl> <dt>ID</dt> <dd>123456</dd> </dl> I attempted: screen.getByRole('term', { name: 'ID' }) as wel ...

Unable to send a PHP array to jQuery

Struggling to transfer PHP-encoded array to JavaScript. homepage.php echo '<script src="script.js"></script>'; $a=array(1,2,3,4,5); echo json_encode($a); ?> script.js: $.ajax({ method: 'GET', url: 'index ...

Utilize jQuery to apply inline styles to dynamically created div elements

I am attempting to apply inline styles to a div using jQuery. The current state of the div, as seen in Firebug, is shown below: https://i.sstatic.net/rqhIc.jpg My goal is to retain the existing CSS while adding margin-left:0 and margin-right:0 to the cur ...

Troubleshooting issue with applying hover effect to child divs

How come when I hover over one of the child items in my parentDiv, the background of the last childDiv changes no matter which child I place my mouse on? for (let i = 0; i < Number(height); i++) { for (let j = 0; j < Number(width); j++ ...