user1627881
user1627881

Reputation: 23

Showing the output of a JSON request

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

Here "str" may be any 2-3 char for an example str = 'nas' then JSON URL : http://en.wikipedia.org/w/api.php?format=json&action=opensearch&search=nas&namespace=0&suggest=

I want to grab all result and put these result in a table

I tried AJAX, JSON, JQUERY Can any one send me working Code for doing this .

My Dummy Code as :-

<!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>

Upvotes: 2

Views: 103

Answers (1)

Eric
Eric

Reputation: 97671

You need to use JSONP to make cross-origin requests:

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);

Note that this is much easier with jQuery.

Upvotes: 1

Related Questions