如何从 API 请求中获取 JSON 到我页面的 javascript 中? [英] How do I get the JSON from an API request into my page's javascript?

查看:18
本文介绍了如何从 API 请求中获取 JSON 到我页面的 javascript 中?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在尝试使用 Web api 在 JavaScript 中返回 JSON 文件.使用 Alchemy API,调用将指向以下 URL:

I'm trying to use a web api to return a JSON file in JavaScript. Using the Alchemy API, the call would be to the following URL:

http://access.alchemyapi.com/calls/url/URLGetTextSentiment?url=http%3A%2F%2Fwww.macrumors.com%2F2013%2F11%2F05%2Fapple-releases-itunes-11-1-3-with-equalizer-and-performance-improvements%2F&apikey=[secret]&outputMode=json

这将对 macrumors 文章进行情感分析.但是,我不确定如何将 JSON 文件实际转换为 javascript.有人知道怎么做吗?

This would run sentiment analysis on a macrumors article. However, I am unsure as to how to actually get the JSON file into javascript. Does anybody know how?

推荐答案

您正在访问的 URL 似乎返回 JSON,因此请使用 ajax get 请求

The URL you're accessing appears to return JSON, so use an ajax get request

使用 jQuery.get():

var Url = 'http://access.alchemyapi.com/calls/url/URLGetTextSentiment?url=http%3A%2F%2Fwww.macrumors.com%2F2013%2F11%2F05%2Fapple-releases-itunes-11-1-3-with-equalizer-and-performance-improvements%2F&apikey=[secret]&outputMode=json'


$.get(
  Url,
  function(data, status, xhr){
    alert(data);
  }
);

这篇关于如何从 API 请求中获取 JSON 到我页面的 javascript 中?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆