.ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﺭﻭﺮﺳ ﮏﯾ ﺯﺍ ﻩﺩﺍﺩ ﺖﺳﺍﻮﺧﺭﺩ ﯼﺍﺮﺑ XMLHttpRequest ﯽﺷ
:ﻢﯿﻨﮐ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ XMLHttpRequest
ﯽﺷ()send ﻭ()open ﯼ
xhttp.open("GET", "ajax_info.txt", true);
xhttp.send();
ﺪﻨﮐ ﯽﻣ ﺺﺨﺸﻣ ﺍﺭ ﺖﺳﺍﻮﺧﺭﺩ ﻉﻮﻧ
(ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ GET ﯼﺍﺮﺑ) ﺪﻨﮐ ﯽﻣ ﻝﺎﺳﺭﺍ ﺭﻭﺮﺳ ﻪﺑ ﺍﺭ ﺖﺳﺍﻮﺧﺭﺩ
(ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ POST ﯼﺍﺮﺑ) ﺪﻨﮐ ﯽﻣ ﻝﺎﺳﺭﺍ ﺭﻭﺮﺳ ﻪﺑ ﺍﺭ ﺖﺳﺍﻮﺧﺭﺩ
:ﺖﺳﺍ ﺭﻭﺮﺳ ﺭﺩ ﻞﯾﺎﻓ ﮏﯾ ﺱﺭﺩﺁ ،open()
ﺵﻭﺭ url ﺮﺘﻣﺍﺭﺎﭘ
xhttp.open("GET", "ajax_test.asp", true);
ﻭ txt ﺪﻨﻧﺎﻣ ،ﺪﺷﺎﺑ ﯽﻠﯾﺎﻓ ﻉﻮﻧ ﺮﻫ ﺪﻧﺍﻮﺗ ﯽﻣ ﻞﯾﺎﻓ
.ﺪﻧﻮﺷ ﻝﺎﺳﺭﺍ ﻥﺎﻣﺰﻤﻫﺎﻧ ﺕﺭﻮﺻ ﻪﺑ ﺪﯾﺎﺑ ﺭﻭﺮﺳ ﯼﺎﻫ ﺖﺳﺍﻮﺧﺭﺩ
()async open ﺮﺘﻣﺍﺭﺎﭘ
xhttp.open("GET", "ajax_test.asp", true);
،ﻥﺎﻣﺰﻤﻫﺎﻧ ﻝﺎﺳﺭﺍ ﺎﺑ
ﺪﯿﻨﮐ ﺍﺮﺟﺍ ﺪﯿﺘﺴﻫ ﺭﻭﺮﺳ ﺦﺳﺎﭘ ﺮﻈﺘﻨﻣ ﻪﮐ ﯽﻟﺎﺣ ﺭﺩ ﺍﺭ ﺮﮕﯾﺩ ﯼﺎﻫ ﺖﭙﯾﺮﮑﺳﺍ
ﺪﯿﻨﮐ ﻪﻠﺑﺎﻘﻣ ﺦﺳﺎﭘ ﺎﺑ ،ﺦﺳﺎﭘ ﻥﺪﺷ ﻩﺩﺎﻣﺁ ﺯﺍ ﺲﭘ
.ﺖﺳﺍ async async=true ﺮﺘﻣﺍﺭﺎﭘ ﯼﺍﺮﺑ ﺽﺮﻓ ﺶﯿﭘ ﺭﺍﺪﻘﻣ
.ﺪﯿﻨﮐ ﻑﺬﺣ ﺩﻮﺧ ﺪﮐ ﺯﺍ ﺍﺭ ﺮﺘﻣﺍﺭﺎﭘ ﻦﯿﻣﻮﺳ ﺖﺣﺍﺭ ﻝﺎﯿﺧ ﺎﺑ ﺪﯿﻧﺍﻮﺗ ﯽﻣ
XMLHttpRequest ﻥﺎﻣﺰﻤﻫ (async=false) ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺍﺮﯾﺯ ﺩﻮﺷ ﯽﻤﻧ ﻪﯿﺻﻮﺗ
POST
ﺎﯾ GET
ﺖﺳﺍﻮﺧﺭﺩ : → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ :ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ URL ﻪﺑ ﺩﺮﻓ ﻪﺑ ﺮﺼﺤﻨﻣ ﻪﺳﺎﻨﺷ ﮏﯾ ،ﺮﻣﺍ ﻦﯾﺍ ﺯﺍ ﯼﺮﯿﮔﻮﻠﺟ ﯼﺍﺮﺑ .ﺪﯿﻨﮐ ﺖﻓﺎﯾﺭﺩ ﺶﮐ ﻪﺠﯿ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ :ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ URL ﻪﺑ ﺍﺭ ﺕﺎﻋﻼﻃﺍ ،ﺪﯿﻨﮐ ﻝﺎﺳﺭﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺖﺳﺍ ﻩﺪﺷ ﻩﺩﺍﺩ ﺢﯿﺿﻮﺗ ﯼﺪﻌﺑ ﻞﺼﻓ ﺭﺩ ،ﺖﺳﺍﻮﺧﺭﺩ ﮏﯾ ﻪﺑ ﺭﻭﺮﺳ ﺦﺳﺎﭘ ﻩﻮﺤﻧ ﻭ ﯼﺩﻭﺭﻭ ﺯﺍ ﺭﻭﺮﺳ ﻩﺩﺎﻔﺘﺳﺍ : → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ .ﺪﯿﻨﮐ ﻪﻓﺎﺿﺍ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ :ﺪﯿﻫﺩ ﺮﯿﯿﻐﺗ ﺖﻓﺎﯾ ﺪﯿﻫﺍﻮﺧ ﺰﯿﻧ ﺎﻤﺷ .ﺩﻮﺷ ﯽﻣ ﻩﺩﺎﻔﺘﺳﺍ ﻊﯾﺮﺳ ﺖﺴﺗ ﯼﺍﺮﺑ async=false ﺯﺍ ﺕﺎﻗﻭﺍ ﯽﻫﺎﮔ .ﺖﺴﯿﻧ → ﺪﯿﻨﮐ ﻥﺎﺤﺘﻣﺍ ﺍﺭ ﻥﺁ ﻥﺎﺗﺩﻮﺧ XMLHttpRequest ﻥﺎﻣﺰﻤﻫ (async=false) ﺖﭙﯾﺮﮑﺳﺍ ﺍﻭﺎﺟ ﺍﺮﯾﺯ ﺩﻮﺷ ﯽﻤﻧ ﻪﯿﺻﻮﺗ ﺪﻨﻫﺩ ﺭﺍﺪﺸﻫ ﻩﺩﺎﻔﺘﺳﺍ ﺩﺭﻮﻣ ﺭﺩ ﺪﻧﻮﺷﯽﻣ ﻪﯿﺻﻮﺗ ﻥﺭﺪﻣ ﻩﺪﻨﻫﺩﻪﻌﺳﻮﺗ ﯼﺎﻫﺭﺍﺰﺑﺍGET
ﻩﺩﺎﺳﻊﯾﺮﺳ ﻭ ﺮﺗﺯﺍ ﺮﺗ ﺎﻫ ﺖﺳﺍﻮﺧﺭﺩ ﺖﻓﺎﯾﺭﺩ
GET
ﻩﺩﺎﺳ ﺖﺳﺍﻮﺧﺭﺩ ﮏﯾ ﻝﺎﺜﻣ
xhttp.open("GET", "demo_get.asp");
xhttp.send();
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xhttp.open("GET", "demo_get.asp");
xhttp.send();
}
</script>
</body>
</html>
ﻝﺎﺜﻣ
xhttp.open("GET", "demo_get.asp?t=" + Math.random());
xhttp.send();
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xhttp.open("GET", "demo_get.asp?t=" + Math.random());
xhttp.send();
}
</script>
</body>
</html>
GET
ﺵﻭﺭ ﺎﺑ ﻝﺎﺜﻣ
xhttp.open("GET", "demo_get2.asp?fname=Henry&lname=Ford");
xhttp.send();
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xhttp.open("GET", "demo_get2.asp?fname=Henry&lname=Ford");
xhttp.send();
}
</script>
</body>
</html>
ﺎﻫ ﺖﺳﺍﻮﺧﺭﺩ ﻝﺎﺳﺭﺍ
POST
ﻩﺩﺎﺳ ﺖﺳﺍﻮﺧﺭﺩ ﮏﯾ ﻝﺎﺜﻣ
xhttp.open("POST", "demo_post.asp");
xhttp.send();
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xhttp.open("POST", "demo_post.asp");
xhttp.send();
}
</script>
</body>
</html>
setRequestHeader()
ﺎﺑ HTTP ﻪﺤﻔﺻﺮﺳ ﮏﯾ ،HT ﻝﺎﺜﻣ
xhttp.open("POST", "ajax_test.asp");
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<button type="button" onclick="loadDoc()">Request data</button>
<p id="demo"></p>
<script>
function loadDoc() {
const xhttp = new XMLHttpRequest();
xhttp.onload = function() {
document.getElementById("demo").innerHTML = this.responseText;
}
xhttp.open("POST", "demo_post2.asp");
xhttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
xhttp.send("fname=Henry&lname=Ford");
}
</script>
</body>
</html>
ﺪﻨﮐ ﯽﻣ ﻪﻓﺎﺿﺍ ﺖﺳﺍﻮﺧﺭﺩ ﻪﺑ ﺍﺭ HTTP ﯼﺎﻫ ﻪﺤﻔﺻﺮﺳ ﻥﺎﻣﺰﻤﻫ ﺖﺳﺍﻮﺧﺭﺩ
false
ﻪﺑ open(
xhttp.open("GET", "ajax_info.txt", false);
onreadystatechange
ﻪﺑ ﯼﺯﺎﯿﻧ ،ﺪﻧﺎﻣ ﯽﻣ ﺭﻭﺮﺳ ﻞﯿﻤﮑ ﻝﺎﺜﻣ
xhttp.open("GET", "ajax_info.txt", false);
xhttp.send();
document.getElementById("demo").innerHTML = xhttp.responseText;
<!DOCTYPE html>
<html>
<body>
<h2>The XMLHttpRequest Object</h2>
<p id="demo">Let AJAX change this text.</p>
<button type="button" onclick="loadDoc()">Change Content</button>
<script>
function loadDoc() {
var xhttp = new XMLHttpRequest();
xhttp.open("GET", "ajax_info.txt", false);
xhttp.send();
document.getElementById("demo").innerHTML = xhttp.responseText;
}
</script>
</body>
</html>