weixin_33744141 2013-04-09 22:35 采纳率: 0%
浏览 47

使用AJAX打开JSON文件

I want to open and parse a JSON file using AJAX ASP/PHP but since I'm really beginner to web programming I am having difficulties connecting their rationales.

I tried w3schools.com to get this example, and I am trying to change inside of the open method (xmlhttp.open("GET","gethint.asp?q="+str,true);). What I tried is that entering a full URL address to the second parameter of method and trying to receive file and parse it. But I couldn't get any file to parse, or I did not understand if I am getting it or not. When I try to open that URL with browser, it downloads a parseable text file and I can make sense of the data. How can I make this automatic?

My current code can be found below:

<!DOCTYPE html>
<html>
<head>
<script>
function showHint(str)
{
    var xmlhttp;

    if (str.length == 0)
    { 
          document.getElementById("txtHint").innerHTML = "";
          return;
    }

    if (window.XMLHttpRequest)
    {// code for IE7+, Firefox, Chrome, Opera, Safari
        xmlhttp=new XMLHttpRequest();
    }
    else
    {// code for IE6, IE5
        xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
    }

    xmlhttp.onreadystatechange=function()
    {
      if (xmlhttp.readyState==4 && xmlhttp.status==200)
        {
            document.getElementById("txtHint").innerHTML=xmlhttp.responseText;
        }
    }

    var url = "https://www.google.com.tr/complete/search?client=hp&hl=en&sugexp=ernk_ir&gs_rn=8&gs_ri=hp&tok=VepIjB5Y4hHneLHcIO5AsQ&cp="+str.length+"&gs_id=1sy&xhr=t&q="+str+"&tch=1&ech=1&psi=FW9kUZu1J6Sw0QXLyIHgBg.1365536538389.1";

    xmlhttp.open("GET",url,true);
    xmlhttp.send();
}
</script>
</head>

<body>

<h3>Start typing a name in the input field below:</h3>
<form action=""> 
    Search Word: <input type="text" id="txt1" onkeyup="showHint(this.value)" />
</form>
<p>Suggestions: <span id="txtHint"></span></p> 

</body>
</html>

I know that it is not complete, I just want to learn that if I am going the wrong way or should I add some other things?

  • 写回答

0条回答 默认 最新

    报告相同问题?

    悬赏问题

    • ¥15 python的qt5界面
    • ¥15 无线电能传输系统MATLAB仿真问题
    • ¥50 如何用脚本实现输入法的热键设置
    • ¥20 我想使用一些网络协议或者部分协议也行,主要想实现类似于traceroute的一定步长内的路由拓扑功能
    • ¥30 深度学习,前后端连接
    • ¥15 孟德尔随机化结果不一致
    • ¥15 apm2.8飞控罗盘bad health,加速度计校准失败
    • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
    • ¥15 谁有desed数据集呀
    • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100