简体   繁体   English

从 Javascript 获取数据

[英]Fetching data from Javascript

How can I get data from a server with no 'Access-Control-Allow-Origin'?如何从没有“Access-Control-Allow-Origin”的服务器获取数据? I can get an Opaque answer only.我只能得到一个不透明的答案。

Problem is that I need to call a public API on a server with no Access-Control-Allow-Origin - I can try to convince site owner to add it, but how do I do it in the meantime?问题是我需要在没有 Access-Control-Allow-Origin 的服务器上调用公共 API - 我可以尝试说服站点所有者添加它,但同时我该怎么做? I am trying to not run a proxy on my own domain.我试图不在我自己的域上运行代理。 The data is personal data (electric power consumption), which I want the browser to download and process, and I do not want my server to see the users private API key, which is stored in localStorage only.这些数据是个人数据(电力消耗),我希望浏览器下载和处理,我不希望我的服务器看到用户的私有 API 密钥,该密钥仅存储在 localStorage 中。 At some point I want to it to become a true web app.在某些时候,我希望它成为一个真正的 web 应用程序。

Fetch with no-cors mode gives me Opaque data, and with cors enabled I get: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.eloverblik.dk/ .使用 no-cors 模式获取给我不透明的数据,并启用 cors 我得到: Cross-Origin Request Blocked: The Same Origin Policy disallows reading the remote resource at https://api.eloverblik.dk/ (Reason: CORS header 'Access-Control-Allow-Origin' missing) (原因:缺少 CORS header 'Access-Control-Allow-Origin')

Any other way to call an API / Get data from a 3rd party site on the web?任何其他方式来调用 API / 从 web 上的第 3 方站点获取数据?

Here is example code:这是示例代码:

fetch( 'https://www.eloverblik.dk', { method: 'GET', redirect: 'follow'})
.then( (response) => { console.log('x'); console.log('Resp: ' + response.type) })
.then((result) => { console.log('Res: ' + result )})
.catch( (error) => { console.error('Error:', error); }) 

Override window.fetch()覆盖window.fetch()

Until the CORS header is added you can implement your own fetch() function, which answers directly in prepared JSON objects.在添加 CORS header 之前,您可以实现自己的fetch() function,它直接在准备好的 Z7A2111C1D7ABBD8 对象中回答。
There is no other workaround unless you have a proxy server, which you wrote isn't an option to you.除非您有代理服务器,否则没有其他解决方法,您编写的代理服务器不是您的选择。

There is no other direct way to talk to the other server.没有其他直接方式可以与其他服务器对话。 A custom browser which ignored the CORS headers would be a security concern and also not realistic, since your visitors will of course have the default security enabled.忽略 CORS 标头的自定义浏览器将是一个安全问题,也不现实,因为您的访问者当然会启用默认安全性。

Having a fake implementation also comes in handy when testing your code, so might be a good idea anyway to implement a mock API, which gives the same results every time.在测试代码时,假实现也很方便,所以无论如何实现一个模拟 API 可能是个好主意,它每次都会给出相同的结果。

You can override window.fetch simply by assigning a different function to it.您可以通过为其分配不同的 function 来覆盖window.fetch See an example below.请参阅下面的示例。

You can of course split the routing and responses into different files to organize your test implementation better.您当然可以将路由和响应拆分到不同的文件中,以更好地组织您的测试实施。 But let's get to the example snippet:但是让我们看一下示例片段:

 // Mock implementation of fetch() to talk to the API window.fetch = function(uri, params) { console.log("fetch() debug implementation called.", uri, params); switch(params.method) { case "GET": if (uri.match(/eloverblik.dk\/?$/i)) { return { type: 'any type you want' }; } else if(uri.match(/eloverblik.dk\/users\/?$/i)) { return { type: 'whatever the API says', response: [{id: 1, name: 'admin'}] }; } break; case "POST": //... break; } }; let res = fetch('https://www.eloverblik.dk', { method: 'GET', redirect: 'follow'}); console.log(res);

Thanks for the help here.感谢您的帮助。

Basically, the answer is, that it is not possible.基本上,答案是,这是不可能的。

The workaround is to use a reverse proxy like nginx and add the header.解决方法是使用 nginx 之类的反向代理并添加 header。

I have contacted the site owner and asked them to add the header.我已联系网站所有者并要求他们添加 header。 It is a public available API, that requires login with an API key you can generate on a website.它是一个公共可用的 API,需要使用可以在网站上生成的 API 密钥登录。

They do run another api server, giving anonymous access to public data.他们确实运行另一个 api 服务器,允许匿名访问公共数据。 That one is run on cloudapp.net.那是在 cloudapp.net 上运行的。 The one where they "forgot" the header has personal electricity consumption measurements - Down to 15m intervals.他们“忘记” header 的地方有个人用电量测量值 - 间隔低至 15m。 It is hosted under t-msedge.net.它托管在 t-msedge.net 下。 Hope they will do the change, so I can do away with the proxy.希望他们会做出改变,所以我可以取消代理。

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

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