2012-03-20 34 views
2

Google APIを使用してサーブレットから呼び出されたブラウザにGoogle検索結果のjsonを表示します。 handleSearchResultはブラウザにresponseTextを表示していません。 また、サーブレットは、htmlのテキストボックスからGoogle APIの検索URL(サーブレット)にクエリワードをマッピングしていません。おかげウェブブラウザでjsonを表示するには

home.html

<html> 
<head> 
<script type="text/javascript"> 
var request; 

function handleSearchResult(){ 
    alert("hanldeRes"); 
    //query.text = resp.items.name; 
    alert(request.responseText); 
    //var resp = eval('('+responseText+')'); 
    //alert(resp.items.title); 
} 

function createHttpRequest() 
{ 
    alert("insideCreateRequest"); 
    if(typeof XMLHttpRequest != "undefined") { 
     request = new XMLHttpRequest();  
    } 
    else if(window.ActiveXObject) { 
     request = new ActiveXObject("Msxml2.XMLHTTP"); 

     if(!request) { 
      request = new ActiveXObject("Microsoft.XMLHTTP"); 
     } 
    } 
    if(request) { 
     request.onreadystatechange = handleSearchResult(); 
     request.open("GET","http://localhost:8080/WebSearchOptimization/SearchService",true); 
     request.send(null); 

    } 
    else { 
     alert("error on Page createHttpRequest"); 
    } 
    return false; 
} 

function home_onclick() 
{ 
    alert("passingRequest"); 
    createHttpRequest();  
} 

</script> 
</head> 
<body> 
<br><br> 
<form method="get" name="form"> 
<input type="text" id="query" name="query"> 
<input type="button" value="Search" id="search" onclick="home_onclick()"> 
<div></div> 
</form> 
</body> 
</html> 
+1

正確にはどのような問題がありますか? –

+0

SearchServiceはJSONレスポンスを返すサーブレットです。request.responseTextを通じてアクセスしますが、結果はリクエストオブジェクトに返されません。 – Priyanka

答えて

0

私はこれが助けるあなたのcode.Hopeにいくつかの変更を行いました。

function handleSearchResult(){ 
    if (request.readyState==4 && request.status==200) 
    {   
     alert(request.responseText); 
    } 
} 

function createHttpRequest() 
{ 
//.... 
if(request) { 
    request.onreadystatechange = handleSearchResult; 
    var q = encodeURI(document.getElementById("query").value); 
    request.open("GET","http://localhost:8080/WebSearchOptimization/SearchService?"+q,true); 
    request.send(null); 
} 
//..... 
} 
関連する問題