2016-08-14 6 views
0

私は、検索用語を入力してWikipedia検索結果のリストを表示する「Wikipedia Viewer」プロジェクトに取り組んでいますが、は完全なものからにはなりません。Wikipedia APIからデータを取得できません

これまで、最初の検索語句を表示するコードを作成しました。しかし、それは動作しません。

マイHTML:

<div class="container-fluid"> 
 

 
<div class="searchAndButtons"> 
 
    
 
<input type="text" id="search"> 
 
    <button class="btn" id="searchBut">Search</button> 
 
    <form action="https://en.wikipedia.org/wiki/Special:Random"><button class="btn">Random Wiki Article</button> 
 
    </form> 
 
    </div> 
 
    <div class="searchResults"> 
 
    </div> 
 
    
 
    
 
    </div>

マイCSS:

.container-fluid{ 
 
    padding:5%; 
 
} 
 
.searchAndButtons{ 
 
    text-align:center; 
 
}

マイJavascriptを:

私が間違っているつもりです

$(document).ready(function(){ 
 
    $("#searchBut").on("click",function(){//this is click handler 
 
    var searchTerm=document.getElementById("#search").value; 
 
    searchTerm=searchTerm.replace(/\s/g,"+"); 
 
    
 
    $.getJSON("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles="+searchTerm+"&rvprop=content&format=json&rvsection=0&rvparse=1",function(json){ 
 
     $(".searchResults").html(JSON.stringify(json)); 
 

 
     }); 
 
    }); 
 
    }); 
 

?コードをCodepenで実行してコンソールを確認すると、 "TypeError:document.getElementById(...)is null"というエラーが表示されます。 codepenのマイプロジェクト - link

答えて

1
$(document).ready(function(){ 
    $("#searchBut").on("click",function(){//this is click handler 
    var searchTerm=document.getElementById("search").value; 
    searchTerm=searchTerm.replace(/\s/g,"+"); 

    $.getJSON("http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles="+searchTerm+"&rvprop=content&format=json&rvsection=0&rvparse=1",function(json){ 
     $(".searchResults").html(JSON.stringify(json)); 

     }); 
    }); 
    }); 

は、これであなたのJSコードを更新します。あなたはあなたの助けのために、次の

$(document).ready(function(){ 
     $("#searchBut").on("click",function(){//this is click handler 
     var searchTerm=document.getElementById("search").value; 
     searchTerm=searchTerm.replace(/\s/g,"+"); 



    $.ajax({ 
       url: 'http://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles="+searchTerm+"&rvprop=content&format=json&rvsection=0&rvparse=1', //your request URL 
       type: 'GET', 
       dataType: 'json', 
       success: function() { alert('hello!'); }, 
       error: function() { alert('boo!'); }, 
       beforeSend: setHeader 
      }); 
      }); 

      function setHeader(xhr) { 
      xhr.setRequestHeader('securityCode', 'Foo'); //your header key and value 
      } 

     }); 
     }); 
+0

感謝を行うことができますが、それはまだ動作しないヘッダを追加するには、次のIDは、「検索」ではない「#search」

UPDATEで値を取得します。コンソールに新しいエラーが表示される - 「クロスオリジン要求がブロックされました:同じオリジンポリシーで、リモートリソースをhttps://en.wikipedia.org/w/api.php?action=query&prop=revisions&titles=c&rvpropで読むことができません。 =理由とフォーマット= json&rvsection = 0&rvparse = 1(理由:CORSヘッダー 'アクセス制御許可の原点'がありません)。 – aks

+0

@aksが私の答えを更新しました。それをチェックしてヘッダーを置くと、 'Access-Control-Allow-Origin:http:// foo.example'をキーと値として使用することができます – Smit

+0

このコードはgetJSON関数の代わりですか?または、このコードをgetJSONに追加する必要がありますか?申し訳ありませんが、質問がばかげているようですが、私は初心者です。 – aks

関連する問題