Organizational Research By

Surprising Reserch Topic

Experts Most Trusted Topic


angularjs http request makes preflight request in controllers but not in app j


angularjs http request makes preflight request in controllers but not in app j  using -'javascript,jquery,angularjs,cors,preflight'

I have an app which communicates with an external API. My app has a controller for each page. If I make a HTTP GET request in one of my controllers, it sends an OPTIONS preflight request, and I receive an error:


  XMLHttpRequest cannot load http://dev.api/. Request header field Authorization is not allowed by Access-Control-Allow-Headers in preflight response.


However, if I copy paste the same code and paste it in my app.js file, it works flawlessly and returns me the data. I found out that this way it also only sends the GET request, and not the OPTIONS request.

I have tried adding the header content type as "text/plain" to my requests but that changes nothing. If I try making the requests with jQuery it works well, sending only the GET request, not matter if the code is in the controller or in the app.js file. Here is the request code (in Angular form):

$rootScope.login = function() {
        var url = "http://dev.api";

        $http({
            url: url,
            method: "GET",
            data: '',
            params: {
                email_address: 'myEmail@email.com',
                password: '111111'
            },
            headers: {
                'Content-Type': 'text/plain'
            }
        })
            .success(function(response) {
                console.log(response);

            })
            .error(function(response) {
                console.log(response);
            });
    }

    

asked Oct 19, 2015 by mcasudhir
0 votes
16 views



Related Hot Questions



Government Jobs Opening


...