简体   繁体   English

AngularJS $ http设置头

[英]AngularJS $http setting headers

I'm trying to set http in AngularJS, but it seems I'm doing something wrong. 我正在尝试在AngularJS中设置http,但看来我做错了。

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:\\*/\\* . 我的主要兴趣是设置accept: json ,但是如果我在Google Chrome浏览器的开发工具中检查请求标头,它始终只会显示Accept:\\*/\\*

EDIT: CORS is set on the server and I'm still getting this error no response. 编辑:在服务器上设置了CORS,但我仍然收到此错误,但无响应。

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.

如果您检查服务器是否已配置Access-Control-Allow-Origin标头,则可能是CORS问题。

Simplify Add as below in your controller class 在控制器类中简化添加,如下所示

@CrossOrigin(origins = "*") public class Controller { @CrossOrigin(origins =“ *”)公共类控制器{

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM