2014-11-11 3 views
-4

방금 ​​JavaScript로 프로그래밍을 시작했고 위키피디아 API를 사용하여 내 자신의 위키 백과 페이지를 만들어야하지만 텍스트 상자에서 데이터를 가져 와서 결과를 표시하는 방법을 검색을 클릭하면 이해할 수 없습니다.디스플레이 위키 백과 검색 방법 은요?

<!DOCTYPE html> 
<html> 

<body> 

<h1>Wikipedia</h1> 
<div id="search1" /> 
<input type="text" name="search" /></b> 
<button id="s1">search</button> 
</div> 
<p id="display"></p> 



<script> 
var xmlhttp = new XMLHttpRequest(); 
var url = "https://community-wikipedia.p.mashape.com/api.php" ; 

xmlhttp.onreadystatechange=function() { 
    if (xmlhttp.readyState == 4 && xmlhttp.status == 200) { 
     myFunction(xmlhttp.responseText); 
    } 
} 
xmlhttp.open("GET", url, true); 
var key = "oCdV3MjLj1mshtyIXwBVzBqRKtY9p1XJNiajsn1vsCETYVLwK3"; 
req.setRequestHeader("X-Mashape-Key", key); 
xmlhttp.send(); 

function Function(response) { 
var a = JSON.parse(response); 
var i; 
text = ""; 
for(i = 0; i < a.length; i++) { 
    text += a[i]+ "<br>"; 
} 

document.getElementById("search1").addEventListener("click",displaysearch); 

function displaysearch(){ 

//display search items here 

} 


} 

</script> 

</body> 
</html> 
+1

그것은 아마 당신이 생략 한'displaysearch' 기능에있을 것입니다. –

+0

예치하지만 내 html에 어떻게 연결합니까? –

답변

0

당신은 당신 후에 설정은 검색 버튼을 클릭하여 HTML에서 iframe을 사용할 수, 또는 당신이 JQuery와 아약스 그건 전화를 GET 사용할 수는 위키 백과 페이지 사용자 액세스에 다시 성공적인 반환하는 경우 통화 요청을 전송 적절한 데이터.

$.ajax({ 
    url: '/path/to/file', 
    type: 'default GET (Other values: POST)', 
    dataType: 'default: Intelligent Guess (Other values: xml, json, script, or html)', 
    data: {param1: 'value1'}, 
}) 
.done(function() { 
    console.log("success"); 
}) 
.fail(function() { 
    console.log("error"); 
}) 
.always(function() { 
    console.log("complete"); 
}); 

jQuery가 두 옵션 중 더 나은 옵션 일 수 있습니다. (제 생각에는)

대안은 (자바 스크립트) iframe이 이런 식으로 뭔가를 할 수 있습니다 :

var ifrm = document.createElement('iframe'); 
ifrm.setAttribute('id', 'ifrm'); 
ifrm.setAttribute('src', 'demo.html'); //URL can go here 
document.body.appendChild(ifrm); //places iFrame at the end of the page 
+0

불행히도 JavaScript에 있어야합니다. –

+0

글쎄, 아마도 iFrame을 사용하여 대안을 시도해 보았습니다. – dhershman