2

JSON URL: http: //en.wikipedia.org/w/api.php?format =json&action=opensearch&search= "+ str +"&namespace = 0&suggest =

ここで、「str」は、たとえばstr ='nas'の場合​​は2〜3文字で、JSON URL: http: //en.wikipedia.org/w/api.php ?format=json&action=opensearch&search=nas&namespace=0&suggest=

すべての結果を取得して、これらの結果をテーブルに入れたい

私はAJAX、JSON、JQUERYを試しました。これを行うための作業コードを誰かに送ってもらえますか。

私のダミーコード:-

<!DOCTYPE html>
<html>
<head>
<script type="application/javascript">
function FillSearchBox(str)
{
    var xmlhttp = new XMLHttpRequest();
    xmlhttp.onreadystatechange=function()
    {

        if (xmlhttp.readyState==4 && xmlhttp.status=200)
        {
            //Pointer never Comes in this Section (If I Debuug I got xmlhttp.status=0 every time) 
            var JSONObject = JSON.parse(xmlhttp.responseText);
        }
    }
    var strr= "http://en.wikipedia.org/w/api.php?format=json&action=opensearch&search="+str+"&namespace=0&suggest=";
    xmlhttp.open("GET",strr,true);
    xmlhttp.send();
}
</script>
</head>
<body>
<form>
<input type="text" name="wikisearch" id=""   onkeyup="FillSearchBox(this.value)" />
</form>
<!-- add Table or Div Here -->
</body>
</html>
4

1 に答える 1

1

クロスオリジン リクエストを作成するには、JSONP を使用する必要があります。

function gotData(d) { alert(d); }

var s = document.createElement('script');
s.src = "http://en.wikipedia.org/w/api.php?format=json&action=opensearch&search="+str+"&namespace=0&callback=gotData";
s.appendTo(document.body);

jQuery を使用すると、これがはるかに簡単になることに注意してください。

于 2012-08-27T14:32:45.593 に答える