【问题标题】:Interface error on Office Script (Excel on Web)Office 脚本上的接口错误(Web 上的 Excel)
【发布时间】:2021-07-01 02:36:05
【问题描述】:

我正在尝试从 Excel 网页版调用外部 API (https://api.fortnox.se/3/customers/2)。

我正在调用的 Web API 返回一个 jason 数据结构,例如此处“检索客户”会话下的“响应”:https://developer.fortnox.se/documentation/resources/customers/

响应数据结构:

{
    "Customer": {
        "@url": "https://api.fortnox.se/3/customers/102",
        "Active": true,
        "Address1": "Halltorpsgatan",
        "Address2": null,
        "City": "KLIPPAN",
        "Comments": null,
        "CostCenter": null,
        "Country": "Sverige",
        "CountryCode": "SE",
        "Currency": "SEK",
        "CustomerNumber": "102",
        "DefaultDeliveryTypes": {
            "Invoice": "PRINT",
            "Offer": "PRINT",
            "Order": "PRINT"
        },
        "DefaultTemplates": {
            "CashInvoice": "DEFAULTTEMPLATE",
            "Invoice": "DEFAULTTEMPLATE",
            "Offer": "DEFAULTTEMPLATE",
            "Order": "DEFAULTTEMPLATE"
        },
        "DeliveryAddress1": null,
        "DeliveryAddress2": null,
        "DeliveryCity": null,
        "DeliveryCountry": null,
        "DeliveryCountryCode": null,
        "DeliveryFax": null,
        "DeliveryName": null,
        "DeliveryPhone1": null,
        "DeliveryPhone2": null,
        "DeliveryZipCode": null,
        "Email": "a.s@example.com",
        "EmailInvoice": "a.s@example.com",
        "EmailInvoiceBCC": "",
        "EmailInvoiceCC": "",
        "EmailOffer": "a.s@example.com",
        "EmailOfferBCC": "",
        "EmailOfferCC": "",
        "EmailOrder": "a.s@example.com",
        "EmailOrderBCC": "",
        "EmailOrderCC": "",
        "Fax": null,
        "GLN": null,
        "GLNDelivery": null,
        "InvoiceAdministrationFee": null,
        "InvoiceDiscount": null,
        "InvoiceFreight": null,
        "InvoiceRemark": "",
        "Name": "Anders Svensson",
        "OrganisationNumber": "",
        "OurReference": "",
        "Phone1": "0435-9249236",
        "Phone2": null,
        "PriceList": "A",
        "Project": "",
        "SalesAccount": null,
        "ShowPriceVATIncluded": false,
        "TermsOfDelivery": "",
        "TermsOfPayment": "",
        "Type": "PRIVATE",
        "VATNumber": "",
        "VATType": "SEVAT",
        "VisitingAddress": null,
        "VisitingCity": null,
        "VisitingCountry": null,
        "VisitingCountryCode": null,
        "VisitingZipCode": null,
        "WWW": "",
        "WayOfDelivery": "",
        "YourReference": "",
        "ZipCode": "264 32"
    }
}

受到 Stackoverflow 帖子的启发:Fetch error on Office Script (Excel on web) 我编写了以下代码:

interface aCustomer {
  Customer: CustomerClass;
}

interface CustomerClass {
  Address1: string;
  Country: string;
}

async function main(workbook: ExcelScript.Workbook): Promise<void> {
  let response = await fetch("https://api.fortnox.se/3/customers/2", {
    method: "GET",
    mode: "no-cors",
    headers: {
      "Access-Token": "XXXX",
      "Client-Secret": "XXXX",
      "Content-Type": "application/json",
      "Accept": "application/json"
    }
  });
  let customer2: aCustomer = await response.json();
  console.log(customer2);
}

但是当我运行它时,它一直抛出错误:“第 21 行:输入意外结束”

第 21 行:让 customer2: Customer = await response.json()

我想我定义了一个错误的接口,或者知道问题可能是什么?谢谢!

【问题讨论】:

  • 这能回答你的问题吗? fetch() unexpected end of input
  • 类型定义还不是问题。我猜你的数据获取有问题。看看我提到的链接
  • 谢谢@Nishant。你的意思是我不应该在 fetch() 方法中使用“no-cors”模式吗?虽然当我尝试不使用“mode: 'no-cors'”时,它在第 11 行“获取失败”“let response = await fetch("api.fortnox.se/3/customers"... 有什么想法可以解决它吗?谢谢!
  • @Max - 你有没有得到一个工作的例子?我也在为此苦苦挣扎

标签: typescript fetch excel-365 office-scripts


【解决方案1】:

您尝试使用的 API 似乎很可能提供给桌面客户端应用程序或服务器应用程序使用,但不是直接从浏览器中的客户端 JavaScript 提供(CORS 可能会成为问题)。

一种可能的解决方法是使用“Fetch Proxy”。简而言之,它是一个简单的 Web 服务器,它将您的原始请求传递给目标 API,并将响应发送回您的客户端 JavaScript。这可能会起作用,因为此 Fetch 代理服务器和目标 API(服务器到服务器通信)之间不会出现 CORS 问题,同时此 Fetch 代理服务器配置为允许来自客户端 JavaScript 的 CORS。

您可以构建和托管自己的 Fetch Proxy 服务器,也可以尝试一些现有的。但是,如果您关心数据隐私或性能,您可能想要构建和托管自己的。这个Stack Overflow post 特别讨论了现有流行的 Fetch Proxy 服务器实现之一(“CORS Anywhere”)。

this article 也对这个 CORS 问题和 Fetch Proxy 想法有一些很好的解释。

【讨论】:

    猜你喜欢
    • 2021-06-26
    • 1970-01-01
    • 1970-01-01
    • 2010-12-11
    • 1970-01-01
    • 1970-01-01
    • 2017-12-06
    • 1970-01-01
    • 2013-11-16
    相关资源
    最近更新 更多