JS不能跨域藉助jquery擷取IP地址的方法,jsjquery
擷取IP地址,JS也可以擷取用戶端IP地址啦,大家知道JS不能跨域,所以這裡藉助了jquery,真的可以實現擷取IP地址,代碼如下,已通過測試:
<script language="javascript" src="http://www.bkjia.com/jslib/jquery/jquery.js"></script> <script language="javascript"> jQuery(function($){ var url = 'http://chaxun.1616.net/s.php?type=ip&output=json&callback=?&_='+Math.random(); $.getJSON(url, function(data){ alert(data.Ip); }); }); </script>
jquery ajax跨域擷取目前使用者的ip地址及相關資訊,
jquery中有$.ajax方法例如
$.ajax({
url:"answer",
type:"POST"
dataType:"json",
success:callback,
error:error
})
請求answer地址到你後台 在後台獲得使用者ip地址後返回輸出資料流,如果這其中沒有發生錯誤就會進入到callback函數中,你就能得到你需要的值!
js跨域訪問js方法,documentdomain="IP地址",怎提示參數無效,大俠解
documen.domain只能嵌套iframe才能跨域。
建議用jsonp方法;
varajaxLogoutScript=document.createElement("script");
ajaxLogoutScript.src=xxxx.url+"?time="+newDate().getTime()+"&jsonCallback=myLogoutcallback";
document.body.appendChild(ajaxLogoutScript);
function myLogoutcallback(data){
if(data){
//dosomething
}
}