Jan Omacka
Jan Omacka

Reputation: 1840

AngularJS $http setting headers

I'm trying to set http in AngularJS, but it seems I'm doing something wrong.

app.controller("mainCtrl", function ($scope, $http) {
    $http({
        method: 'GET',
        url: '***',
        headers: {
            'Accept': 'application/json',
            'Content-Type': 'application/json'
        }
    }).success(function (data, status, headers, config) {
        console.log("data", data);
        console.log("status", status);
        console.log("headers", headers);
        console.log("config", config);
    }).error(function (data, status, headers, config) {
        console.log("data", data);
        console.log("status", status);
        console.log("headers", headers);
        console.log("config", config);
    })
})

My main interest is to set accept: json, but if I check the request headers in Google Chrome's dev tools it always just says Accept:\*/\*.

EDIT: CORS is set on the server and I'm still getting this error no response.

XMLHttpRequest cannot load http://inpro.smid.co.cz:8080/InproRestApi/webresources/entities.regions. Request header field Access-Control-Allow-Origin is not allowed by Access-Control-Allow-Headers in preflight response.

Upvotes: 1

Views: 655

Answers (2)

Narayana
Narayana

Reputation: 11

Simplify Add as below in your controller class

@CrossOrigin(origins = "*") public class Controller {

Upvotes: 0

Niels
Niels

Reputation: 1603

It could be a CORS issue did you check if the server has configured the Access-Control-Allow-Origin header.

Upvotes: 1

Related Questions