ADAL - Incorporating JavaScript within an Angular JS single page application to access external APIs via APIGEE and encountering CORS issues

I recently developed a Single Page Application (SPA) and utilized Azure AD for user storage along with the ADAL-JavaScript library according to instructions found on a to integrate with my AngularJS code. The authentication process worked smoothly, however, when attempting to call a third-party API exposed through APIGEE, I encountered the following error messages:

Failed to load resource: the server responded with a status of 502 (Bad Gateway)

XMLHttpRequest cannot load http://webapiexposedusingapigee. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin '' is therefore not allowed access. The response had HTTP status code 502.

Upon checking in Fiddler, I noticed the following fault string: "faultstring=Received 405 Response without Allow Header"

and a warning stating:

Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at http://test.apigee.net/v1/selectop/myapi. This can be fixed by moving the resource to the same domain or enabling CORS.

I have already added the necessary headers on APIGEE:

 <Header name="Access-Control-Allow-Origin">*</Header>
            <Header name="Access-Control-Allow-Credentials">true</Header>
            <Header name="Access-Control-Allow-Headers">Origin, X-Requested-With, Content-Type, Accept</Header>
            <Header name="Access-Control-Max-Age">3628800</Header>
            <Header name="Access-Control-Allow-Methods">GET, PUT, POST, DELETE, OPTION</Header>

Any assistance with resolving this issue would be greatly appreciated.

Thank you.

Answer №1

If your WebAPI endpoint has the correct CORS setup, it will be able to accept requests. I'm not too familiar with APIGEE, but you can follow this link for a guide on how to enable CORS support: .

When working with AngularJS, make sure to specify the use of xdomain to send headers:

app.factory('contactService', ['$http', function ($http) {
var serviceFactory = {};

var _getItems = function () {
    $http.defaults.useXDomain = true;
    delete $http.defaults.headers.common['X-Requested-With'];
    return $http.get('http://adaljscors.azurewebsites.net/api/contacts');
};

serviceFactory.getItems = _getItems;

return serviceFactory;

}]);

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

Update the style class of an <img> element using AJAX

My success with AJAX enables PHP execution upon image click. However, I seek a real-time visual representation without page reload. Thus, I aim to alter <img> tag classes on click. Presently, my image tag resembles something like <img title="< ...

Use two fingers to scroll up and down on the screen

I am currently developing a sketch web application (using angular) that utilizes one finger gestures for drawing. My goal is to enable vertical scrolling in the sketch content by using two fingers. However, when attempting to scroll with two fingers, Safa ...

Restricting data list values in AngularJS

Currently, I am facing a performance issue while trying to display approximately 2000 values retrieved through an API call using DataList in AngularJS. The page becomes extremely slow due to the large number of items being rendered. Is there a way to optim ...

Retrieve a JSON object by making a request to a URL with specific parameters

Struggling with a common coder's mental block: The idea: Imagine entering a URL like www.mysite.com/getStuff?name=Jerry&occupation=Engineer&Id=12345 Rather than receiving a webpage, the goal is to get back a json object for parsing on anoth ...

Troubleshooting CORS Problems with jQuery and Spring Boot

Recently, I have encountered an issue with my front-end pages that are developed using jQuery 3.2.1 and running by npm. Here is a snippet of how it is set up: "start": "http-server -a localhost -p 8000 -P http://localhost:8080 -c-1 --cors ./app" When mak ...

Incorporating traditional Javascript classes for modeling in React development

Can traditional JavaScript classes be utilized in models within the MVC framework while using React, as opposed to relying on Redux or contexts & reducers which may impact reusability? If this approach is feasible, how can we efficiently 'subscribe&ap ...

Execute a factory function with a parameter

HTML: <div class="container"> <div> <div ng-controller="LogPhoneMainController" ng-init="init()"> <div class="row"> <div class="col-sm-3 col-md-3 col-lg-3"> <h2>{{header}}</h2& ...

Is there a way to retrieve a label's text using their class name within a loop?

I have multiple labels sharing the same classname, each with different text. My goal is to iterate through them and extract the text they contain. Here is the example markup: <label class="mylabel pull-left"> Hello </label> <label class="m ...

What is the method to retrieve a javascript object from within a string template?

I am trying to find a way to map data from an object into a string template using JavaScript. Here is an example of the string and object I am working with: const menu = { breakfast: { description:'something' } meal: { d ...

Jstree: Whenever I refresh my tree, the checkboxes do not reset to default and remain unchecked

After attempting to implement the following code: $("#jstree_demo_div").jstree("destroy").empty(); Although it successfully removes the checked nodes and reloads the tree, it fails to apply the new changes. Any advice on how to resolve this issue would b ...

Is lazy initialization for useState accompanied by any disadvantages?

The documentation for useState explains: If the initial state is the result of an expensive computation, you may provide a function instead, which will be executed only on the initial render While I see the reasoning behind using a function for an expe ...

Why does the Angular page not load on the first visit, but loads successfully on subsequent visits and opens without any issues?

I am currently in the process of converting a template to Angular that utilizes HTML, CSS, Bootstrap, JavaScript, and other similar technologies. Within the template, there is a loader function with a GIF animation embedded within it. Interestingly, upon ...

Obtaining the desired element from a function without relying on an event

Recently, I've been working on a sidebar with several links <sidebar-link href="/dashboard" icon="HomeIcon" :is-active="isActive()" /> <sidebar-link href="/test" icon="TestIcon" :is-active=&qu ...

Animate failed due to the appending and adding of class

$('#clickMe').click(function() { $('#ticketsDemosss').append($('<li>').text('my li goes here')).addClass('fadeIn'); }); <link href="http://s.mlcdn.co/animate.css" rel="stylesheet"/> <script ...

Guide to implementing tooltips for disabled <li> elements in AngularJS

I have a list of items that are displayed using the following code: <li class="dropdown-item" data-toggle="tooltip" uib-tooltip="tooltip goes here" data-placement="left" data-ng-repeat="result in items.results.contextValues.rows " ...

When clicking on HTML input fields, they do not receive focus

I am facing a puzzling issue where I am unable to access the input fields and textareas on my HTML form. The JS, HTML, and CSS files are too large for me to share here. Could someone provide guidance on what steps to take when troubleshooting this unusual ...

When attempting to import Three.js canvas on Github Pages, a 404 error is received

While attempting to host my webpage with a three.js background, I encountered an issue where everything loads properly when hosted locally, but nothing loads when pushed to GitHub pages - only the HTML is visible. I am utilizing Vite to package my code an ...

Fetching JSON data using Javascript/JQuery

My goal is to access JSON data from an online web service that queries a MySQL database for a specific string and use Javascript to present it on an HTML page. My challenge lies in effectively displaying the retrieved information. The relevant part of my ...

Acquiring the root URL with the $location service in AngularJS

I am facing a situation where I have a specific URL structure like the one shown below. http://localhost:8080/test#/users/list Upon further investigation, I discovered the following information: $location.url() returns -> "users/list" $location.path( ...

What is the syntax for assigning a scope name like $scope.username.firstname in AngularJS?

Check out the snippet below This is a sample controller: angular.module("sampleApp").controller("transactionController",function($scope){ $scope.audit.lob = { "type": "select", "name": "Service", "value": "-S ...