Multiple Ajax Calls using AngularJS Issues
NickName:user2951483 Ask DateTime:2016-12-01T09:39:37

Multiple Ajax Calls using AngularJS Issues

I am fairly new to AngularJS. A particular project necessitated using a MVC and I choose angular for it's ability to template and do seamless DOM insertions.

I'm loading a local config file to load specific API endpoints (created by the client) and then doing two AJAX calls to populate an object. Problem is that the second ajax call isn't working. It shows a console error similar to what you would see if you've encountered a CORS issue but I know by testing the endpoint via a REST client that the endpoint and parameters are correct (also tested it using a regular JQuery ajax call). I do not want to revert to jQuery code and would prefer to use angular to accomplish this.

This is the error I get:

XMLHttpRequest cannot load https://this/url/endpoint. Response for preflight is invalid (redirect)

Code:

var object = {};

angular.module('myApp').controller('myController', function ($scope, $http) {

    $http.get('data.json').then(function (data) {

        var url1 = data.url;
        var httpMethod1 = data.http1;
        var url2 = data.url2;
        var httpMethod2 = data.http2;

        $http({
            method: httpMethod1,
            url: url1,
            headers: {"token": token},
            dataType: 'json'
        }).success(function (data, status, headers, config) {

            object.attr1 = data.att0;
            object.attr2 = data.att1;

        }).error(function (data, status, headers, config) {
            alert('failed');
        });
        //THIS ONE FAILS  
        $http({
            url: httpMethod2,
            type: url2,
            headers: {"token": token},
            data: { "value" : "value"}
            dataType: 'json',
        }).success(function (data, status, headers, config) {

            object.attr3 = data.att0;
            object.attr4 = data.att1;

        }).error(function (data, status, headers, config) {
            alert('failed');
        });
    });
});

Copyright Notice:Content Author:「user2951483」,Reproduced under the CC 4.0 BY-SA copyright license with a link to the original source and this disclaimer.
Link to original article:https://stackoverflow.com/questions/40901246/multiple-ajax-calls-using-angularjs-issues

More about “Multiple Ajax Calls using AngularJS Issues” related questions

Multiple Ajax Calls using AngularJS Issues

I am fairly new to AngularJS. A particular project necessitated using a MVC and I choose angular for it's ability to template and do seamless DOM insertions. I'm loading a local config file to l...

Show Detail

Subsequent ajax calls in angularJs

I'd like to make subsequent ajax calls in angularJs inside a service. I've tried with something like this: var requests = [ {'url': 'call1'}, {'url': 'call2'}, {&#

Show Detail

Chaining Ajax calls in AngularJs

I would like to make multiple Ajax calls in a chain. But I also would like to massage the data after each call before making the next call. In the end, when All calls are successful, I would like t...

Show Detail

make multiple http calls synchronously in AngularJs

I am using AngularJs in my project. I have to make multiple http calls and then return the consolidated result to my view. How to achieve this using AngularJs? Please let me know since I am not an

Show Detail

AngularJS loading multiple items by AJAX call

I have a listing page. WHere each listing has comments. I am using AngularJS routes to load the content http://example.com/#mycategory The Problem: When user navigates to different category (Say:...

Show Detail

AngularJS: Using $q to fire ajax calls synchronously

Is it possible to use $q to fire ajax requests synchronously in AngularJS? I have a long list of vehicles, each vehicle has events associated with them and I need to retrieve the eventdetails of e...

Show Detail

Using multiple ajax calls in one javascript function

Thanks in advance for any help. Is it bad practice and/or inefficient to use multiple $.ajax calls in one javascript function? I've been working on one and have a simple testing environment set u...

Show Detail

AngularJS : multiple asynchronous AJAX calls

I am calling multiple ajax calls but the code only reaches the API after all the ajax calls are executed. Javascript: function test = function(){ var entity = {}; entity.Number =...

Show Detail

multiple calls to ajax simultaneously

I'm trying to make multiple calls to Ajax, i have fields like time intervals and no of calls to ajax under that time period. Now the problem is, while making multiple calls to same Ajax, there may be

Show Detail

Ajax, multiple calls

I wonder is it a good practice to do multiple Ajax calls?

Show Detail