试图从4chan API中获得

发布于 2025-01-31 16:49:22 字数 1378 浏览 1 评论 0原文

我正在尝试向此端点提出一个请求 https://a.4cdn.org/boars。 json 4chan api https://github.com/4chan/4chan/4chan-api 。

文档说:

CORS is supported from origins boards.4chan.org or boards.4channel.org, via http:// or https://. 

当我在浏览器中访问此端点时,我会得到JSON,但是当我在Angular应用中提出请求时,我会收回响应:

headers: Object { normalizedNames: Map(0), lazyUpdate: null, headers: Map(0) }
message: "Http failure response for https://a.4cdn.org/boards.json: 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: "https://a.4cdn.org/boards.json"

这是我的代码:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class BoardsService {

  constructor(private http: HttpClient) { }

  getBoards() {
    this.http.get('https://a.4cdn.org/boards.json').subscribe({
      next: (data) => {
        console.log(data);
      },
      error: (err) => {
        console.log(err);
      },
      complete: () => {
        console.log('complete');
      }
    });
  }
}

有人可以使此功能正常吗?

我希望我不需要创建API,只使用另一个API

I'm trying to make a GET request to this endpoint https://a.4cdn.org/boards.json of the 4chan API https://github.com/4chan/4chan-API.

The documentation says:

CORS is supported from origins boards.4chan.org or boards.4channel.org, via http:// or https://. 

When I access this endpoint in my browser I get the JSON just fine but when I make a request in my Angular app I get a response back with:

headers: Object { normalizedNames: Map(0), lazyUpdate: null, headers: Map(0) }
message: "Http failure response for https://a.4cdn.org/boards.json: 0 Unknown Error"
name: "HttpErrorResponse"
ok: false
status: 0
statusText: "Unknown Error"
url: "https://a.4cdn.org/boards.json"

Here's my code:

import { Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http';

@Injectable({
  providedIn: 'root'
})
export class BoardsService {

  constructor(private http: HttpClient) { }

  getBoards() {
    this.http.get('https://a.4cdn.org/boards.json').subscribe({
      next: (data) => {
        console.log(data);
      },
      error: (err) => {
        console.log(err);
      },
      complete: () => {
        console.log('complete');
      }
    });
  }
}

Is anyone able to get this working?

I hope I don't need to create an API just use another API

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

对不⑦ 2025-02-07 16:49:22

您已经遇到了 cors 问题。

本质上,除非从4chan上托管的页面完成,否则无法从浏览器中使用该4chan API,否则无法使用。解决方法是从服务器中获取,因为它不是Web浏览器,并且不强制执行CORS规则。

您不必自己托管服务器,也有许多现有的服务旨在规避CORS(请参阅 cors-nywhere )。请注意,对于生产代码来说,这不是一个好主意,例如,有人可以劫持CORS重定向服务器并做坏事。

建议您自己制作自己的API,或者,或者,请托管一些简单的服务器以绕过CORS(例如CORS-ANYWHERE)。

You have run into a CORS issue.

Essentially, this 4chan API cannot be used from the browser, unless done from a page hosted on 4chan. The workaround is to fetch from a server, as it's not a web browser, and does not enforce CORS rules.

You don't have to host the server yourself, there are plenty of existing services designed to circumvent CORS (see cors-anywhere for example). Note that this is not a good idea for production code, for example, someone could hijack the CORS redirection server and do bad stuff.

You are recommended to make your own API, or alternatively, host some simple server to get around CORS (like cors-anywhere).

旧伤还要旧人安 2025-02-07 16:49:22

所有第三方API CORS错误的最简单解决方案是通过您的后端进行呼叫。

The easiest solution to all third party API CORS errors is to make the call through your backend.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文