ajax实例 html页面中用js调用一个php文件

来源:互联网 发布:eve 作战网络 编辑:程序博客网 时间:2024/05/28 17:07

var xmlHttp;

function getIPByPHP(ip)
{
 if (ip.length==0){
  return;
 }
 xmlHttp=GetXmlHttpObject()
 if (xmlHttp==null){
    alert ("Browser does not support HTTP Request");
    return;
 } 
 var url="ip.php";
 url=url+"?ip="+ip;
 url=url+"&sid="+Math.random();
 xmlHttp.onreadystatechange=stateChanged; 
 xmlHttp.open("GET",url,true);
 xmlHttp.send(null);
}

function stateChanged(){ 
 if (xmlHttp.readyState==4 || xmlHttp.readyState=="complete"){ 
  //document.getElementById("txtHint").innerHTML=xmlHttp.responseText;
  var json = ""+xmlHttp.responseText;
  var obj = eval("(" + json + ")");
  
  lat = obj.latitude;//纬度
  
  longitude = obj.longitude;//经度
  
  countryCode = obj.country_code;
  country = obj.country_name;
  city = obj.city;
  
  ip = obj.ip;
  
  ipLatLng = new google.maps.LatLng(lat,longitude);
  
  chunZhenCountry = obj.chunzhen_country;
  chunZhenArea = obj.chunzhen_area;
 }
}

function GetXmlHttpObject()
{
var xmlHttp=null;
try
 {
 // Firefox, Opera 8.0+, Safari
 xmlHttp=new XMLHttpRequest();
 }
catch (e)
 {
 // Internet Explorer
 try
  {
  xmlHttp=new ActiveXObject("Msxml2.XMLHTTP");
  }
 catch (e)
  {
  xmlHttp=new ActiveXObject("Microsoft.XMLHTTP");
  }
 }
return xmlHttp;
}





用jquery吧
<script src="http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js"></script>
<script>
$(function(){
  $("#ajaxad").html("载入中").load("ajax.php");
});
</script>
<div id="ajaxad"></div>


原创粉丝点击