使用带有Angular和Typescript的Ebay API

问题描述 投票:1回答:2

我在使用Ebay API和Angular应用程序时遇到问题。我正在做的是从Ebay获取数据,这是以JSON的形式。这一切都很好,但我一直收到这个错误:

Uncaught ReferenceError: _cb_findItemsByKeywords is not defined

请参阅下面的我的TypeScript代码:

import { Component, OnInit, Injectable } from '@angular/core';
import { HttpClient } from '@angular/common/http'
import { url } from 'inspector';

@Component({
  selector: 'app-web-scrape',
  templateUrl: './web-scrape.component.html',
  styleUrls: ['./web-scrape.component.css']
})
@Injectable({
  providedIn: 'root'
})
export class WebScrapeComponent implements OnInit {

  _cb_findItemsByKeywords(root) {
    var items = root.findItemsByKeywordsResponse[0].searchResult[0].item || [];
    var html = [];
    html.push('<table width="100%" border="0" cellspacing="0" cellpadding="3"><tbody>');
    for (var i = 0; i < items.length; ++i) {
      var item     = items[i];
      var title    = item.title;
      var pic      = item.galleryURL;
      var viewitem = item.viewItemURL;
      if (null != title && null != viewitem) {
        html.push('<tr><td>' + '<img src="' + pic + '" border="0">' + '</td>' +
        '<td><a href="' + viewitem + '" target="_blank">' + title + '</a></td></tr>');
      }
    }
    html.push('</tbody></table>');
    document.getElementById("results").innerHTML = html.join("");
  }  // End _cb_findItemsByKeywords() function

  ngOnInit() {
    var url = "https://svcs.ebay.com/services/search/FindingService/v1";
    url += "?OPERATION-NAME=findItemsByKeywords";
    url += "&SERVICE-VERSION=1.0.0";
    url += "&SECURITY-APPNAME=ConorRaf-TheEazyT-PRD-d55b8f6ea-a1f297a5";
    url += "&GLOBAL-ID=EBAY-US";
    url += "&RESPONSE-DATA-FORMAT=JSON";
    url += "&callback=_cb_findItemsByKeywords";
    url += "&REST-PAYLOAD";
    url += "&keywords=harry%20potter";
    url += "&paginationInput.entriesPerPage=3";
    console.log("hello from ts");
    var s=document.createElement('script'); // create script element
    s.src = url;
    console.log(s.src)
    document.body.appendChild(s);

  }
}

代码编译很好,并返回某种JSON对象,但我在控制台中收到此错误:

enter image description here

错误似乎在回调中:

enter image description here

HTML仅供参考:

<html>
<head>
<title>eBay Search Results</title>
</head>
<body>
<h1>eBay Search Results</h1>
<div id="results"></div>


</body>
</html>
angular typescript ebay-api
2个回答
1
投票

你为什么用这个script的东西?你应该坚持使用Angular方式并使用HttpClient服务。

您的代码如下所示:

this.http.get(url).subscribe(root => _cb_findItemsByKeywords(root));

而不是在script标签中添加请求。

您可以通过简单的方式首先了解它的工作原理:

this.http.get(url).subscribe(root => console.log(root));

并从那里构建逻辑。

顺便说一下,Angular方式不是像你那样直接操纵DOM。 Angular的大部分功能来自模板和数据绑定。我建议你去探索Angular doc,以便很好地掌握使用Angular时发生的事情。


0
投票

是的,如上所述,你不应该使用带角度的脚本。这违背了语言的原则。我强烈建议查看Angular Docs以更好地了解如何使用组件。

另外我认为你是初学者,所以Tutorialspoint Angular可以为新手程序员提供另一个好的资源

© www.soinside.com 2019 - 2024. All rights reserved.