简体   繁体   English

在JavaScript中使用JSON http请求

[英]Using JSON http request in JavaScript

I'm currently working on a coding project that entails pulling images, titles, thumbnails, etc. from what I believe is a JSON Http Request (although I'm not sure) and adding them to a webpage, which will then be styled with CSS. 我目前正在从事一个编码项目,该项目需要从我认为是JSON Http Request(尽管我不确定)中提取图像,标题,缩略图等,然后将其添加到网页中,然后使用CSS。 I have minimal experience using JSON, jQuery, AJAX, etc. I can't figure out how to take the string I'm provided with and turn it in to a usable JavaScript object that I can add to my html page. 我对JSON,jQuery,AJAX等的使用经验很少。我不知道如何将提供的字符串转换为可用的JavaScript对象,然后将其添加到html页面中。

The URL that I'm supposed to pull this request from can be found here . 我应该从中提取此请求的URL可以在这里找到。

When I go to that URL I'm presented with the following: 当我转到该URL时,会看到以下内容:

{"welcome_message":"Hi there! Good luck!","endpoints":["/videos","/articles"],"supportedParameters":{"startIndex":"The index of the first record to return in the response. Used for pagination. Min is 0, max is 300","count":"The number of records to return in the response. Min is 1, max is 20."},"sampleRequest":"http://ign-apis.herokuapp.com/articles?startIndex=30\u0026count=5"}

If you'll notice there is a URL at the end of this JSON that leads here . 如果您会注意到,此JSON末尾有一个URL指向此处 When I follow that URL I'm presented with the following: 当我遵循该URL时,会看到以下内容:

{"count":5,"startIndex":30,"data":[{"thumbnails":[{"url":"http://assets1.ignimgs.com/2016/11/07/akumajpg-f485c3_compact.jpg","size":"compact","width":306,"height":172},{"url":"http://assets1.ignimgs.com/2016/11/07/akumajpg-f485c3_medium.jpg","size":"medium","width":466,"height":262},{"url":"http://assets1.ignimgs.com/2016/11/07/akumajpg-f485c3_large.jpg","size":"large","width":626,"height":352}],"metadata":{"headline":"Opinion: Despite Low Sales, Street Fighter 5 Isn’t Going Anywhere","networks":["ign"],"state":"published","slug":"opinion-despite-low-sales-street-fighter-5-isnat-going-anywhere","subHeadline":"Call off the funeral, folks.","publishDate":"2017-02-08T19:00:52+0000","articleType":"article"},"tags":["feature","capcom","fighting","game","pc"]},{"thumbnails":[{"url":"http://assets1.ignimgs.com/2017/02/07/gamingawardsannouncement-ign-blogroll-lisahause-1486502878639_compact.jpg","size":"compact","width":306,"height":172},{"url":"http://assets1.ignimgs.com/2017/02/07/gamingawardsannouncement-ign-blogroll-lisahause-1486502878639_medium.jpg","size":"medium","width":466,"height":262},{"url":"http://assets1.ignimgs.com/2017/02/07/gamingawardsannouncement-ign-blogroll-lisahause-1486502878639_large.jpg","size":"large","width":626,"height":352}],"metadata":{"headline":"SXSW Gaming Awards Voting Ends Soon","networks":["ign"],"state":"published","slug":"sxsw-gaming-awards-voting-ends-soon-2","publishDate":"2017-02-08T19:00:25+0000","articleType":"article","subHeadline":"The 2017 SXSW Gaming Awards are open to public voting until February 10th.\r\n"},"tags":["blogroll-promoted-ad","sxsw"]},{"thumbnails":[{"url":"http://assets1.ignimgs.com/2017/02/08/daily-deals-blogrollsackboy-1486574960774_compact.png","size":"compact","width":306,"height":172},{"url":"http://assets1.ignimgs.com/2017/02/08/daily-deals-blogrollsackboy-1486574960774_medium.png","size":"medium","width":466,"height":262},{"url":"http://assets1.ignimgs.com/2017/02/08/daily-deals-blogrollsackboy-1486574960774_large.png","size":"large","width":626,"height":352}],"metadata":{"headline":"Daily Deals: Get LittleBigPlanet 3 for Free with a Discounted PlayStation Plus 1-Year Membership!","networks":["ign"],"state":"published","slug":"daily-deals-get-littlebigplanet-3-for-free-with-a-discounted-playstation-plus-1-year-membership","subHeadline":"How can you say no to joining PS Plus (or extending your current membership) when that cute, smiling Sackboy is waiting for you?","publishDate":"2017-02-08T18:00:23+0000","articleType":"article"},"tags":["daily-deals"]},{"thumbnails":[{"url":"http://assets1.ignimgs.com/2017/02/08/davidsyd-1280-1486576588705_compact.jpg","size":"compact","width":306,"height":172},{"url":"http://assets1.ignimgs.com/2017/02/08/davidsyd-1280-1486576588705_medium.jpg","size":"medium","width":466,"height":262},{"url":"http://assets1.ignimgs.com/2017/02/08/davidsyd-1280-1486576588705_large.jpg","size":"large","width":626,"height":352}],"metadata":{"headline":"Legion Review","networks":["ign"],"state":"published","slug":"legion-series-premiere-review","publishDate":"2017-02-08T17:57:01+0000","articleType":"article","subHeadline":"The premiere episode of FX’s new series Legion, debuting Wednesday night, offers up a whole new take on the X-Men world."},"tags":[]},{"thumbnails":[{"url":"http://assets1.ignimgs.com/2016/12/12/theoa-blogroll-1481576018102_compact.jpg","size":"compact","width":306,"height":172},{"url":"http://assets1.ignimgs.com/2016/12/12/theoa-blogroll-1481576018102_medium.jpg","size":"medium","width":466,"height":262},{"url":"http://assets1.ignimgs.com/2016/12/12/theoa-blogroll-1481576018102_large.jpg","size":"large","width":626,"height":352}],"metadata":{"headline":"Netflix Renews The OA for Season 2 and Love for Season 3","networks":["ign"],"state":"published","slug":"netflix-renews-the-oa-for-season-2-and-love-for-season-3","subHeadline":"The streaming services continues to order more original content. ","publishDate":"2017-02-08T17:28:12+0000","articleType":"article"},"tags":["news","brit-marling","company","drama","jason-isaacs"]}]}

I've done some research that makes me think that these are JSON Http requests. 我进行了一些研究,使我认为这些是JSON Http请求。 The problem I'm facing is that I'm not sure how to turn them in to usable JavaScript objects that I can use in my webpage. 我面临的问题是我不确定如何将它们转换为可以在网页中使用的可用JavaScript对象。 To try to troubleshoot this problem myself I watched "JavaScript and JSON" on Lynda.com, as well as "JavaScript and AJAX" to try to figure out what to do with these, that didn't bear any meanful fruit. 为了自己解决此问题,我在Lynda.com上观看了“ JavaScript和JSON”,以及“ JavaScript和AJAX”以试图弄清楚如何处理这些问题,但结果没有任何意义。 The next step I took was to read the section about JSON in "JavaScript and jQuery: The Missing Manual". 我的下一步是阅读“ JavaScript和jQuery:缺少的手册”中有关JSON的部分。 In my book it says to use the function $.getJSON(url, data, callback); 在我的书中,它说使用$ .getJSON(url,data,callback);函数。 I'm assuming that I'd add the url(s) that I posted above as the argument for url. 我假设我要添加上面发布的url作为url的参数。 I'm not sure what to pass for "data" and "callback" though. 我不确定要为“数据”和“回调”传递什么。

To make a long question short, could someone give me an example of how to take the JSON string from the provided link(s) and turn the information into usable objects I can arrange and style? 长话短说,有人可以给我一个例子,说明如何从提供的链接中获取JSON字符串,然后将信息转换为我可以排列和设置样式的可用对象吗?

Please note that I'm new to both programming and also to StackOverflow. 请注意,我既不熟悉编程,也不熟悉StackOverflow。 I realize that there are strict guidelines about how to properly ask questions on here. 我意识到这里有关于如何正确提问的严格指南。 If anything about my question is inappropriate please kindly let me know. 如果关于我的问题的任何内容不合适,请告诉我。 Any and all help is greatly appreciated! 任何帮助都将不胜感激!

If you're using $.getJSON , you don't need to do anything to turn the JSON string into usable data, it does that automatically for you (that's the difference between $.get and $.getJSON ). 如果使用$.getJSON ,则无需执行任何操作即可将JSON字符串转换为可用数据,它会自动为您执行此操作(这就是$.get$.getJSON之间的区别)。

However, you won't be able to call the API directly from Javascript, because of the same-domain rule that prohibits making AJAX calls to different domains. 但是,由于同一域规则禁止对不同域进行AJAX调用,因此您将无法直接从Javascript调用API。 You'll need to provide a proxy on your own server that performs the call; 您需要在自己的服务器上提供代理来执行呼叫; you can use Simple PHP Proxy for this. 您可以为此使用简单PHP代理

The data argument is an object containing any parameters you need to send to the API. data参数是一个对象,其中包含您需要发送到API的所有参数。 It's optional and you can leave it out if not needed. 它是可选的,如果不需要,可以将其省略。

The callback argument is your function that does something with the returned object (which will already be parsed for you by $.getJSON ). callback参数是您的函数,它对返回的对象(已由$.getJSON进行解析)执行$.getJSON For example: 例如:

$.getJSON(url, function(result) {
    alert(result.welcome_message);
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM