PDA

View Full Version : سوال: اجرا نشدن کد AJAX



hamiddehban
سه شنبه 24 اردیبهشت 1392, 22:45 عصر
سلام دوستان من دارم آجاکس رو کار می کنم. و مثالهایی که از اون دارم نه روی wamp ، نه روی xamp و نه روی هاستم اجرا میشن . یعنی یه ایرادی تو کد هست که من نمی فهمم :گریه:
کد فایل urlpost.html


<html><body>
<h1> This is a Test Ajax </h1.>
<h2> It loads a web page into this local one! </h2>
<div id='info'> This will be replaced ! </div>

<script>
onerror=errorHandler
function errorHandler(msg,url,line)
{
out ="Sry, but an error encountered \n"
out+="it says: " + msg + "\n"
out+="it's url is:" + url + "\n"
out+="it occured in line: " + line + "\n"
out+="So plz click on ok ;)"
alert(out)
return true
}
params="url=google.com"
request=new ajaxRequest()
request.open("POST","urlpost.php",true)

request.setRequestHeader("Content-type","application/x-www-form-urlencoded")
request.setRequestHeader("Content-length",params.length)
request.setRequestHeader("Connection","close")

request.onreadystatechange=new function()
{
if(this.readyState==4)
{
if(this.status==200)
{
if(this.response.Text!=null)
{
document.getElementById('info').innerHTML=this.res ponseText
}
else
{
alert("Ajax Error: No data received\n ByBye")
}
}
else
{
alert("Ajax Error: " + this.statusText)
}
}
else
{
document.write("Not happening bro!")
}
}

request.send(params)
function ajaxRequest(params)
{
try
{
var request=new XMLHttpRequest()
}
catch(e1)
{
try
{
request=new ActiveXObject("Msxml2.XMLHTTP")
}
catch(e2)
{
try
{
request=new ActiveXObject("Microsoft.XMLHTTP")
}
catch(e3)
{
request=false
}
}
}
return request
}

</script>

</body></html>


کد فایل urlpost.php


<?php

if(isset($_POST['url']))
echo file_get_contents('http://'.sanitize_string($_POST['url']));

function sanitize_string($var)
{
$var=strip_tags($var);
$var=htmlentities($var);
return stripslashes($var);
}

?>




تنها چیزی که با آزمون و خطا فهمیدم اینه که this.readyState==4 نمیشه :تشویق: لطفاً کمک کنید گیج شدم حسابی :(

dousti_design
چهارشنبه 25 اردیبهشت 1392, 00:01 صبح
<html><body>
<h1> This is a Test Ajax </h1.>
<h2> It loads a web page into this local one! </h2>
<div id='info'> This will be replaced ! </div>

<script src="//ajax.googleapis.com/ajax/libs/jquery/1.9.1/jquery.min.js"></script>
<script type="text/javascript" language="javascript">
$.ajax({
url: "urlpost.php",
type: "xml",
cache: false
}).done(function( html ) {

document.getElementById("info").innerHTML = html;


});
</script>

</body></html>