简体   繁体   English

无法使用html表单元素从JSON获取信息以及使用Wikimedia从getJSON方法获取信息

[英]Can't get info from JSON using a html form element and getJSON method using wikimedia

I'm trying to submit something to search with the form and pull info from the j son 我正在尝试提交一些内容以使用表格搜索并从j儿子那里获取信息

 $("#target1").submit(function(e) { e.preventDefault(); var x = document.getElementById("input").value; var url = "https://en.wikipedia.org/w/api.phpaction=query&format=json&prop=revisions&list=search&titles=&rvprop=content&srsearch=" + x; var the = function(teh) { $("#target2").text(teh.continue.continue); } $.getJSON(url, the); }); 
 <script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script> <form action="" method="" id="target1"> <input class='text-center' type='text' id='input' placeholder='Search Wikipedia' required> </form> <div class='text-center'> <p id='target2'></p> </div> 

example link of json: https://en.wikipedia.org/w/api.php?action=query&format=jsonfm&prop=revisions&list=search&titles=&rvprop=content&srsearch=cats json的示例链接: https ://en.wikipedia.org/w/api.php ? action = query & format = jsonfm & prop = revisions & list = search & titles =& rvprop = content & srsearch =cats

I am trying to get the info from the titles but im just using the "continue" to make it shorter. 我正在尝试从标题中获取信息,但是我只是使用“ continue”来使其更短。
full code at http://codepen.io/nunez7890/pen/aWWXqy 完整代码位于http://codepen.io/nunez7890/pen/aWWXqy

Looks like it's CORS issue. 看起来是CORS问题。 Refer here for solution Wikipedia API + Cross-origin requests 请参阅此处以获取解决方案Wikipedia API +跨域请求

Add origin=* to the URL query params. 将origin = *添加到URL查询参数。

$("#target1").submit(function(e) {
  e.preventDefault();
  var x = document.getElementById("input").value;
  var url = "https://en.wikipedia.org/w/api.php?origin=*&action=query&format=json&prop=revisions&list=search&titles=&rvprop=content&srsearch=" + x;
  var the = function(teh) {
    $("#target2").text(teh.continue.continue);
  }
  $.getJSON(url, the);
});

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

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