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?