如何解决CORS错误在消费应用Angular2用双簧管的API?(How to solve CORS

2019-10-31 08:08发布

我有API返回这样的数据:

{"t":"point","id":817315,"tableid":141,"classid":142,"state":0,"loc":[6850735.34375,24501674.0039063]}
{"t":"line","id":817314,"tableid":204,"classid":2102,"loc":[[6850335.8828125,24501476.50390625],[6850341.48828125,24501476.8828125],[6850362.171875,24501492.21484375],[6850387.4140625,24501508.86328125],[6850442.66796875,24501545.69921875],[6850502.34375,24501584.0078125],[6850558.3359375,24501619.37109375],[6850611.375,24501654.73046875],[6850671.05078125,24501693.04296875],[6850708.62109375,24501687.1484375],[6850735.34375,24501674.00390625]]}

我试图用双簧管,但没有成功必读。 这个怎么做? 随着下面的代码,我得到错误在这个问题的结尾:

    oboe('http://localhost:19100/pn/api/v1/fetch?cgid=22&north=6853000.0&east=24505000&south=6850000.0&west=24500000.0')
    .node('*', (row) => {
        console.log(row);

        return oboe.drop;
    })
    .done(() => {

        return oboe.drop;
    })
    .fail((err) => {
        // error
        console.log('oboe fail ', err);
        return oboe.drop;
    });

访问的XMLHttpRequest在' HTTP://本地主机:19100 / PN / API / V1 /取cgid进行= 22&北= 6853000.0&东= 24505000&南= 6850000.0及西= 24500000.0? '从原点' 的http://本地主机:4200 '已被封锁CORS政策:否“访问控制允许来源”标头出现在所请求的资源。 [ HTTP://本地主机:4200 /主]

当我与镀铬头取这个样子:

Request URL: http://localhost:19100/pn/api/v1/fetch?cgid=22&north=6853000.0&east=24505000&south=6850000.0&west=24500000.0
Request Method: GET
Status Code: 200 OK
Remote Address: [::1]:19100
Referrer Policy: no-referrer-when-downgrade
Content-Type: application/json
Date: Mon, 25 Feb 2019 19:55:41 GMT
Server: Microsoft-HTTPAPI/2.0
Transfer-Encoding: chunked
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Accept-Encoding: gzip, deflate, br
Accept-Language: en-US,en;q=0.9
Cache-Control: max-age=0
Connection: keep-alive
Host: localhost:19100
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; Win64; x64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/72.0.3626.119 Safari/537.36
cgid: 22
north: 6853000.0
east: 24505000
south: 6850000.0
west: 24500000.0
fetch?cgid=22&north=6853000.0&east=24505000&south=6850000.0&west=24500000.0
/pn/api/v1

Answer 1:

正如@威廉 - 罗汉提到的,这是一个CORS问题。 如果你有控制权的API主机的话,你可以启用CORS标头允许跨域请求就像一个你正试图使。 但是,启用CORS的想法应该不能掉以轻心,因为这将您的API暴露在几种常见的潜在的XSS漏洞。 CORS配置应该进行彻底的测试,并定期复查。

作为替代方案,角确实允许您指定用于发展方式的代理配置。 例如:

// package.json
...
    "scripts": {
        "start": "ng serve --proxy-config proxy.conf.js"
    }
...

// proxy.conf.js
const PROXY_TARGET = 'http://localhost:19100';
module.exports = [{
    context: ['**/api/**/*'], // May be able to get away with '**/api/**' or less here; haven't tested
    target: PROXY_TARGET,
    changeOrigin: true,
    secure: false,
    logLevel: 'debug',
    autoRewrite: true
}];

然后你的请求目标URI应改为使用当前页域(” /pn/api/v1/... “)而不是原始域(” http://localhost:19100/pn/api/v1/... “)。

当您使用开发模式,这将只适用。 如果您的API和UI在任何其他环境中通过不同的域访问,那么你将需要实现这些环境的解决方案为好,如反向代理或上述CORS。



文章来源: How to solve CORS error consuming an API using oboe in Angular2 app?