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

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

问题描述

我试图使用Web API,JavaScript中返回一个JSON文件。使用炼金术API,调用将以下网址:

<$p$p><$c$c>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

这将运行在一个加拿大家园文章情感分析。不过,我不确定如何真正得到JSON文件转换为JavaScript。有谁知道怎么样?


解决方案

您正在访问自然会返回JSON,所以使用一个Ajax GET 请求网址

使用 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'
$获得(
  网址,
  功能(数据,状态XHR){
    警报(数据);
  }
);

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

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?

解决方案

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

With 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请求到我的网页的JavaScript的JSON?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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