goodreads API阻止了跨域请求:javascript中的错误

时间:2019-03-14 19:40:52

标签: javascript ajax

我正在尝试使用javascript中的Goodreads列出书籍,但出现跨域请求阻止错误。

var data = [{ divisionName: "Adboom", divisionId: '000', merchantName: "Jaydox LTD", merchantId: '020', entityName: "beautifullyyoungskin.net", entityId: '500' }, { divisionName: "Adboom", divisionId: '000', merchantName: "Jaydox LTD", merchantId: '020', entityName: "thinbodydiet.com", entityId: '501' }, { divisionName: "Adboom", divisionId: '000', merchantName: "Jaydox LTD", merchantId: '020', entityName: "youthfulskincare.net", entityId: '502' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu (3d Secure)", entityId: '503' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu (Non-3d)", entityId: '504' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu - ST (Non-3d)", entityId: '505' }, { divisionName: "Adult", divisionId: '100', merchantName: "Occonti Ltd", merchantId: '040', entityName: "datinginthe.eu ST (3d Secure)", entityId: '506' }],
    keys = ["division", "merchant", "entity"],
    result = data
        .reduce((r, o) => {
            keys.reduce((t, k) => {
                var temp = (t.children = t.children || []).find(p => p.id === o[k + 'Id']);
                if (!temp) {
                    t.children.push(temp = { name: o[k + 'Name'], id: o[k + 'Id'] });
                }
                return temp;
            }, r);
            return r;
        }, {})
        .children;

console.log(result);

如何解决它并获取结果。

我在这里浏览了许多链接,但似乎没有任何帮助。谁能帮助我解决问题。

致谢

1 个答案:

答案 0 :(得分:1)

对于像我这样的人,他们失去了所有希望找到解决这个问题的希望,并全心全意地来到了这里,但是在SO中没有答案,这是可以遵循的方法。

背景。自2015年以来,一直有人要求解决此问题(根据Goodreads论坛)。至少要花6年的时间,考虑到要解决问题有多么容易,否则请放弃希望,因为不幸的是这不会发生。

旁注。不要费心向Goodreads提出JSON的答复。 Goodreads除了HTML iframe不会给您任何帮助。顺便说一句,这不是您可以猜测的JSON响应。相反,使用XML响应format=xml)。不用担心,Node中有一个库可以帮助您:xml2js

解决方案。与许多CORS问题一样,这可以通过反向代理(假设您无法更改服务器)来解决。

有很多选择。我发现cors-anywhere带有指向(https://cors-anywhere.herokuapp.com/的公共服务器,但是它也可以安装在您的计算机上。对于Goodreads,请求网址应采用以下格式:https://cors-anywhere.herokuapp.com/https://www.goodreads.com/book/isbn/{isbn}?format=xml&user_id={userid}&key={apikey}

我最终使用axios library进行连接(按照本Goodreads developers post的建议)

我对Angular的解决方案(但我想它可以很容易地转换为任何JS系统,包括纯Web)。

. . .
import axios from 'axios';
import { AxiosInstance } from "axios";
import * as xml2js from 'xml2js'; // To parse XML to JSON-like
. . .


export class YourService {

  private axiosClient: AxiosInstance;

  constructor(private httpClient: HttpClient) {

    this.axiosClient = axios.create({
            timeout: 3000,
            headers: {
                "X-Initialized-At": Date.now().toString()
            }
        });

  }


  async getBook(isbn: string):Promise<YourBookObject> {
    . . .
    var config = {headers: {"X-Requested-With" : "XMLHttpRequest"}};
    var url = "https://cors-anywhere.herokuapp.com/https://www.goodreads.com/book/isbn/"+isbn+"?format=xml&user_id="+userid+"&key="+apikey
    await this.axiosClient.get(url, config )
    .then(function (response) {
          if(response.status == 200){
            xml2js.parseString( response.data, function (err, result) {
              console.log("XML-JSON Parse")
              console.log(result); // Prints JSON object!
              . . .
            });
          }
        })
    .catch(function (error) {
          console.log(error);
    })
    . . . // additional checks to response and return
  }

}

希望我能帮助,尽管迟到了一年:)