Javascript 显示JSON请求的输出

Javascript 显示JSON请求的输出,javascript,ajax,json,Javascript,Ajax,Json,JSON URL:“+str+”&名称空间=0&建议= 例如,这里的“str”可以是任何2-3个字符 str='nas' 然后是JSON URL: 我想获取所有结果,并将这些结果放在一个表中 我尝试了AJAX、JSON和JQUERY 有人能给我发工作代码吗 我的虚拟代码为:- <!DOCTYPE html> <html> <head> <script type="application/javascript"> function FillSearc

JSON URL:“+str+”&名称空间=0&建议=

例如,这里的“str”可以是任何2-3个字符 str='nas' 然后是JSON URL:

我想获取所有结果,并将这些结果放在一个表中

我尝试了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>

函数FillSearchBox(str)
{
var xmlhttp=new XMLHttpRequest();
xmlhttp.onreadystatechange=函数()
{
if(xmlhttp.readyState==4&&xmlhttp.status=200)
{
//指针从未出现在本节中(如果我每次都使用xmlhttp.status=0)
var JSONObject=JSON.parse(xmlhttp.responseText);
}
}
var strr=”http://en.wikipedia.org/w/api.php?format=json&action=opensearch&search=“+str+”&namespace=0&suggest=“;
open(“GET”,strr,true);
xmlhttp.send();
}

您需要使用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更容易做到这一点。

您不能执行跨域JSON请求。mediawiki提供jsonp吗?是的,mediawiki提供jsonp-您可以使用
回调
查询参数和要调用的函数名。