使用ajax从API检索JSON数据(错误:无访问控制 - 允许 - 来源)

时间:2017-08-25 03:38:33

标签: jquery ajax api cross-domain

我必须使用基本授权访问API,

请注意,我无权访问存储API的服务器。我无法修改api并使用ASP.net创建

我在提供凭据后访问浏览器上的api网址时可以查看json数据,

然而,当我试图通过AJAX请求检索数据时 我收到此错误

OPTIONS https://apis url/HR/api/employees 405 (Method Not Allowed)

XMLHttpRequest cannot load https://apis url/HR/api/employees. 
Response to preflight request doesn't pass access control check: No 'Access-Control-Allow-Origin' header is present on the requested resource. 
Origin 'http://xxx.xx.xx.xx' is therefore not allowed access. The response had HTTP status code 405.

如何以任何合法方式检索json数据?

这是我的代码

function getDataF()
            {
                var _token = $("input[name='_token']").val();
                $.ajax({
                    xhrFields: {
                        withCredentials: true
                    },
                    headers: { 
                        'Authorization': "Basic encryptedvalue_here"
                    },
                    url: 'https://apis url/HR/api/employees',
                    type: 'GET',
                    dataType: 'json',
                    contentType: "application/json",
                    success: function(response){  
                        console.log(response);
                    }
                });
            }

0 个答案:

没有答案
相关问题