نوشته شده توسط
moghtada
سلام اساتید عزیز.
تو یه سایت با زدن دکمه جستجو، محتوا نشون داده یعنی یه request با متد get ارسال میشه و یه response دریافت و نشون داده میشه. همه اینارو تو بخش network مرور گر میشه دید. میخوام با جاوا اسکریپ و بدون زدن اون دکمه این کارو انجام بدم یعنی request رو بفرستم بدون زدن دکمه. آیا راهی هست بشه این کارو انجام داد؟
با Ajax همین کار رو انجام میدن.
کد HTML:
<!DOCTYPE html>
<html>
<head>
<script>
function getResponse(xhttp)
{
document.getElementById("statusText").innerHTML = xhttp.statusText;
document.getElementById("responseText").innerHTML = xhttp.responseText;
document.getElementById("responseHeaders").innerHTML = xhttp.getAllResponseHeaders();
}
function loadFunction()
{
var xhttp;
xhttp=new XMLHttpRequest();
xhttp.onreadystatechange = function()
{
if (this.readyState == 4)
{
getResponse(this);
}
};
xhttp.open("GET", "https://jsonplaceholder.typicode.com/todos/1", true);
xhttp.send();
}
</script>
</head>
<body onload="loadFunction()">
Status:
<br>
<div id="statusText">
</div>
<br>
Response Text:
<br>
<div id="responseText">
</div>
<br>
Response Headers:
<br>
<div id="responseHeaders">
</div>
</body>
</html>