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

How to efficiently store and manage a many-to-many relationship in PostgreSQL with TypeORM

I have a products entity defined as follows: @Entity('products') export class productsEntity extends BaseEntity{ @PrimaryGeneratedColumn() id: number; //..columns @ManyToMany( type => Categories, categoryEntity => cat ...

How come I keep running into the "is not a function" issue when trying to use the generateRequest function with Polymer's iron-ajax

Oops, it seems like there was an error: Uncaught TypeError: this.$.ajax.generateRequest is not a function. The issue seems to be in assets-ajax.html at line 23. <dom-module id="assets-pull"> <style> </style> <template> <but ...

Expanding a table by including a new column using Node.js and Knex

Currently building a service for my router using Node.js and Knex, but I'm struggling to add a column to an existing table. Any assistance would be greatly appreciated. Even though I am working with PostgreSQL, I believe the solution will be similar r ...

Navigating from the root view to a (grand)child view with Angular2: Mastering Direct Links

I am currently exploring Angular 2 and have encountered a perplexing issue that I need assistance in debugging. An Illustrative Scenario In my project, I have an AppComponent that manages the fundamental level routing. Each subsequent level of routing is ...

What steps are necessary to add a Contact Us form to my HTML website?

Currently, I am searching for a way to add a "Contact Us" section to my website that is completely HTML-based. I believe the best approach would involve using a PHP script to handle sending emails from a form on the Contact Us page, but I am not familiar ...

Arranging serialized information from a tabular format and retrieving specific data

: I'm currently attempting to utilize an AJAX POST request to send data from a dynamic webpage that includes information gathered from a table form, a date selection box, and comment text input. The webpage is generated as a PHP file on a GET request ...

Is it possible for images underneath to receive focus when hovering over them?

I'm struggling with a layout of thumbnails on my page. We'll refer to them as A, B, C, etc. They are currently displayed like this: A, B, C, D, E, F, G, H, I, J, K, L, M, N... and so on. When you hover over one thumbnail, it enlarges by 2.5 t ...

Error encountered: index.html:17 - An InvalidStateError was thrown when attempting to execute the 'send' function on the XMLHttpRequest object. The object's state must be OPENED in order to send the Ajax request

When attempting to run index.html on a local host with xampp, an error is encountered: index.html:17 Uncaught InvalidStateError: Failed to execute 'send' on 'XMLHttpRequest': The object's state must be OPENED.sendAjax @ index.html: ...

Is it possible to exclusively target a child div using JavaScript in CSS, without affecting the parent div?

I'm in the process of developing a calendar feature where users can select a specific "day" element to access a dropdown menu for time selection. The functionality is working fine, but I've encountered an issue. When a user selects a time from th ...

Sorting arrays in javascript

My JavaScript array is structured like this: var data_tab = [[1,id_1001],[4,id_1004],[3,id_1003],[2,id_1002],[5,id_1005]] I am looking to organize and sort them based on the first value in each pair: 1,id_1001 2,id_1002 3,id_1003 4,id_1004 5,id_1005 ...

The Javascript setTimeout Function Prolongs Its Operation

Currently, I have a web app index page that performs two main tasks. Firstly, it geocodes the user's location and stores it in a JavaScript variable called "variableToSend." Secondly, it sends this data to mySQL using a PHP script after a delay of 10 ...

Highchart displays results for each individual line without the use of commas

I am interested in creating a chart using Highchart with three lines. The first two lines will display data similar to [4.12, 3.34, 5.45] / [3.45, 5.45, 3.23], while the third line should be displayed without any commas, showing results like [7, 4, 2]. Can ...

Effectively accessing the Templater object within a user script for an Obsidian QuickAdd plugin

I have developed a user script to be used within a QuickAdd plugin Macro in Obsidian. The Macro consists of a single step where the user script, written in JavaScript following the CommonJS standard, is executed. Within this script, there is a task to gene ...

Enhancing ng-model by clicking on a list item

I currently have a textbox on my webpage. <input ng-model="meetingData.client.ClientName" id='txtClient' placeholder="Add new client" type="text"> along with a list of items that I want to interact with. Is there a ...

Converting an unbroken series of string values into organized key-value pairs for easy reference

I assure you this is not a duplicated question. Despite my attempts with JSON.parse(), it seems to be ineffective. Here's the issue at hand: I recently received assistance from an answer that was both crucial and enlightening. However, the code prov ...

Creating Dynamic Menus in Angular

My current setup is as follows: I am using a Dynamo DB table with 3 items, each representing a link in a menu. To fetch and display this data, I have set up a Lambda function which scans the table and sends a JSON response through an API gateway. Within ...

A guide on retrieving the selected option from a dropdown menu with React Material UI

Utilizing Material UI React, I am constructing a dropdown menu containing various options. My concern is: if I choose two options from different dropdowns within the menu, how can I intercept or store which option was selected? Below is a snippet of my co ...

A guide to using jqGrid: Retrieve the value of a particular cell by double clicking on a row

Is there a way to implement a feature where users can double click on any part of a row and have it open a new HTML page based on the specific content in a cell? For example, I have a table with different counties in New York listed in separate rows: Coun ...

What is the best way to define an Angular UI Modal without using a controller?

Can the Angular UI Modal controller/object be declared outside of its triggering controller in a separate file without polluting the global namespace? Is it possible to declare the modal controller like a regular controller and pass parameters in from the ...

Utilize AJAX to connect to a remote domain from an extension

I am currently working on creating a Chrome extension that will utilize AJAX to fetch data from a specific webpage and then generate notifications based on the content of that page. The webpage I am targeting is a ticketing system, and my goal is to deter ...