Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Welcome To Ask or Share your Answers For Others

Categories

0 votes
137 views
in Technique[技术] by (71.8m points)

javascript - 如何使用API??密钥进行Ajax调用?(How to use an API Key for an Ajax call?)

I am trying to include an API key for the first time from New York Times API ( http://developer.nytimes.com/ ) and use ajax to fetch news from it to populate a local website but I'm not seeing any results.(我正在尝试首次从纽约时报API( http://developer.nytimes.com/ )中包含一个API密钥,并使用ajax从中获取新闻以填充本地网站,但没有看到任何结果。)

I was told to Make sure your API key is set in the URL's query parameters but I'm not sure how to do it.(有人告诉我确保在URL的查询参数中设置您的API密钥,但是我不确定该怎么做。) ?api-key=your-key Here is what I have done:(这是我所做的:) // Built by LucyBot. www.lucybot.com var url = "https://api.nytimes.com/svc/search/v2/articlesearch.json"; url += '?' + $.param({ 'api-key': "111111111111111111111111111111" }); $.ajax({ url: url, method: 'GET', }).done(function(result) { console.log(result); }).fail(function(err) { throw err; }); I need to see the url in json format for various stories such as business, technology, etc and use them for an ajax call.(我需要查看json格式的网址,以了解各种故事,例如业务,技术等,并将其用于ajax调用。)   ask by Developer101 translate from so

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
Welcome To Ask or Share your Answers For Others

1 Reply

0 votes
by (71.8m points)

Try this I am getting data from this(试试这个我从这里获取数据)

var url = "https://api.nytimes.com/svc/search/v2/articlesearch.json"; url += '?' + $.param({ 'api-key': "11111111111111111111111" }); $.ajax({ url: url, method: 'GET', dataType: 'JSON', success: function(data) { console.log(data) }, error: function(err) { console.log('error:' + err) } }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> you can also try like as follows(您也可以尝试如下) var url = "https://api.nytimes.com/svc/search/v2/articlesearch.json"; $.ajax({ url: url, method: 'GET', dataType: 'JSON', data: { 'api-key': '11111111111111111' }, success: function(data) { console.log(data) }, error: function(err) { console.log('error:' + err) } }) <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

与恶龙缠斗过久,自身亦成为恶龙;凝视深渊过久,深渊将回以凝视…
OGeek|极客中国-欢迎来到极客的世界,一个免费开放的程序员编程交流平台!开放,进步,分享!让技术改变生活,让极客改变未来! Welcome to OGeek Q&A Community for programmer and developer-Open, Learning and Share
Click Here to Ask a Question

...