对API的跨源Ajax请求返回500(内部服务器错误)响应

时间:2017-08-18 09:00:32

标签: angularjs http cors http-status-code-500

我收到错误回复' 500(内部服务器错误)'当我对api进行GET调用时。这是错误消息:

  

XMLHttpRequest无法加载   http://10.10.14.188:4800/api/v1/healthMonitor/DeviceStatus。响应   预检请求没有通过访问控制检查:否   '访问控制允许来源'标题出现在请求的上   资源。起源' http://192.168.4.217'因此是不允许的   访问。响应的HTTP状态代码为500。

...虽然当我尝试使用邮递员或使用curl命令时我得到了回复。

在这个电话中,我必须在标题中传递一个id。这是代码:

this.getState = function () {
        return $http({
                method: 'GET',
                url: 'http://10.10.14.188:4800/api/v1/healthMonitor/DeviceStatus',
                headers: {'Jab_Device_id': '80f1374192a6d6eb9ebf48e2eba18366','Content-Type': 'application/json'}
                });
    };

另外,我尝试使用Ajax但是我也遇到了同样的错误。这是代码:

$.ajax({
        url: 'http://10.10.14.188:4800/api/v1/healthMonitor/DeviceStatus',
        headers: {
          'Jab_Device_id':'80f1374192a6d6eb9ebf48e2eba18366'  
        },
        method: 'GET',
        withCredentials: true,
        success: function(data){
          console.log('succes: '+data);
        }
      });

2 个答案:

答案 0 :(得分:1)

var url = "http://10.10.14.188:4800/api/v1/healthMonitor/DeviceStatus";
$http({
    method: 'JSONP',
    url: url,
    headers: {'Jab_Device_id': '80f1374192a6d6eb9ebf48e2eba18366','Content-Type': 'application/json'}
}).
success(function(status) {
    //your code when success
}).
error(function(status) {
    //your code when fails
});

CORS是跨域资源共享,如果您尝试从一个域访问另一个域,则会出现此错误。

尝试使用JSONP。在您的情况下,JSONP应该可以正常工作,因为它只使用GET方法。

尝试这样的事情:

来自this answer

但是,如果您无法执行所有其他操作,则可以访问其余的api更新过滤器以启用CORS请求

答案 1 :(得分:0)

这与api服务器无关。此应用程序在ngnix服务器上运行,并且还需要从客户端启用cors。我刚刚将这段代码添加到默认文件中并且有效。

location / {
         if ($request_method = 'OPTIONS') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            #
            # Custom headers and headers various browsers *should* be OK with but aren't
            #
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            #
            # Tell client that this pre-flight info is valid for 20 days
            #
            add_header 'Access-Control-Max-Age' 1728000;
            add_header 'Content-Type' 'text/plain; charset=utf-8';
            add_header 'Content-Length' 0;
            return 204;
         }
         if ($request_method = 'POST') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
         }
         if ($request_method = 'GET') {
            add_header 'Access-Control-Allow-Origin' '*';
            add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
            add_header 'Access-Control-Allow-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
            add_header 'Access-Control-Expose-Headers' 'DNT,X-CustomHeader,Keep-Alive,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Content-Range,Range';
         }
    }
相关问题