asp ajax跨域提交數(shù)據(jù)
更新時間:2008年11月24日 21:37:59 作者:
需要一個js函數(shù).當(dāng)每出來一條記錄時,就把ip替換為城市
星期五寫了個分類信息的小東東!在數(shù)據(jù)庫里只有ip地址,一般訪客不太清楚IP地址來源于哪個城市.如果在表里多一個列保存城市又沒有真實性可言.如果能把IP地址變成城市多好呀.當(dāng)然可以去down下數(shù)據(jù)庫.可就為這個需求覺得有點浪費.還好了有好多網(wǎng)站提供查詢.如果能把它的結(jié)果變成我的.問題解決.
需要一個js函數(shù).當(dāng)每出來一條記錄時,就把ip替換為城市:
<script type="text/javascript">
function queryAddress(strID){
try{
var qIp=document.getElementById("ip_"+strID);
var qUrl='http://ip.wanvee.cn/GetIp.ashx?ipstr='+qIp.firstChild.nodeValue;
var ajax=new Ajax.Request(qUrl,{
method:'get',
onSuccess:function(strResponse){
var resContent=strResponse.responseText;
var strStruct=resContent.substring(resContent.lastIndexOf(",")+1,resContent.length);
qIp.innerHTML=strStruct.split(" ")[0];
}
});
}catch(e){}
}
</script>
寫一個測試用例:
<span id="ip_2">221.123.123.123</span><script type="text/javascript">queryAddress('2')</script>
<span id="ip_3">221.123.123.123</span><script type="text/javascript">queryAddress('3')</script>
<span id="ip_4">221.123.123.123</span><script type="text/javascript">queryAddress('4')</script>
羅列一下我找的幾個查詢網(wǎng)址:
http://www.ip.cn/getip.php?action=queryip&ip_url=221.123.123.123
http://ip.wanvee.cn/GetIp.ashx?ipstr=221.123.123.123
以上兩個只返回文本
http://www.youdao.com/smartresult-xml/search.s?type=ip&q=221.123.123.123
這個返回XML
寫完了,意識到一個問題.ajax不支持跨域提交.這也不成問題:我們用腳本寫一個頁面.用msxml的load方法裝載目標(biāo)網(wǎng)址!就可以搞到我們感興趣的數(shù)據(jù)!參考此貼:http://topic.csdn.net/t/20030619/12/1933920.html
js代碼稍微變一下:
function queryAddress(strID){
try{
var qIp=document.getElementById("ip_"+strID);
var qUrl='queryiplocal.asp?ip='+qIp.firstChild.nodeValue;
var ajax=new Ajax.Request(qUrl,{
method:'get',
onSuccess:function(strResponse){
qIp.innerHTML=strResponse.responseText;
}
});
}catch(e){}
}
下面的是ASP文件源碼:
<%
Response.ContentType="text/xml"
Response.Charset="GB2312"
Dim strIP,strPattern
strIP=Request.QueryString("ip")
strPattern="^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
If strIP="" Or IsNumeric(strIP) then
Response.End()
ElseIf Not serRegValidate(strPattern,strIP) then
Response.End()
End If
Dim strURL:strURL="http://www.youdao.com/smartresult-xml/search.s?type=ip&q="
set parser=Server.CreateObject("MSXML2.DOMDocument")
parser.async=false
parser.ValidateOnParse=true
parser.setProperty "ServerHTTPRequest",true
parser.load(strURL)
if parser.parseError.errorCode<>0 then
Response.End()
end if
set currNode=parser.selectNodes("http://product")
Dim strLocal:strLocal=currNode.item(0).selectSingleNode("location").text
Response.Write Split(strLocal," ")(0)
%>
需要一個js函數(shù).當(dāng)每出來一條記錄時,就把ip替換為城市:
復(fù)制代碼 代碼如下:
<script type="text/javascript">
function queryAddress(strID){
try{
var qIp=document.getElementById("ip_"+strID);
var qUrl='http://ip.wanvee.cn/GetIp.ashx?ipstr='+qIp.firstChild.nodeValue;
var ajax=new Ajax.Request(qUrl,{
method:'get',
onSuccess:function(strResponse){
var resContent=strResponse.responseText;
var strStruct=resContent.substring(resContent.lastIndexOf(",")+1,resContent.length);
qIp.innerHTML=strStruct.split(" ")[0];
}
});
}catch(e){}
}
</script>
寫一個測試用例:
<span id="ip_2">221.123.123.123</span><script type="text/javascript">queryAddress('2')</script>
<span id="ip_3">221.123.123.123</span><script type="text/javascript">queryAddress('3')</script>
<span id="ip_4">221.123.123.123</span><script type="text/javascript">queryAddress('4')</script>
羅列一下我找的幾個查詢網(wǎng)址:
http://www.ip.cn/getip.php?action=queryip&ip_url=221.123.123.123
http://ip.wanvee.cn/GetIp.ashx?ipstr=221.123.123.123
以上兩個只返回文本
http://www.youdao.com/smartresult-xml/search.s?type=ip&q=221.123.123.123
這個返回XML
寫完了,意識到一個問題.ajax不支持跨域提交.這也不成問題:我們用腳本寫一個頁面.用msxml的load方法裝載目標(biāo)網(wǎng)址!就可以搞到我們感興趣的數(shù)據(jù)!參考此貼:http://topic.csdn.net/t/20030619/12/1933920.html
js代碼稍微變一下:
復(fù)制代碼 代碼如下:
function queryAddress(strID){
try{
var qIp=document.getElementById("ip_"+strID);
var qUrl='queryiplocal.asp?ip='+qIp.firstChild.nodeValue;
var ajax=new Ajax.Request(qUrl,{
method:'get',
onSuccess:function(strResponse){
qIp.innerHTML=strResponse.responseText;
}
});
}catch(e){}
}
下面的是ASP文件源碼:
復(fù)制代碼 代碼如下:
<%
Response.ContentType="text/xml"
Response.Charset="GB2312"
Dim strIP,strPattern
strIP=Request.QueryString("ip")
strPattern="^[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}.[0-9]{1,3}$"
If strIP="" Or IsNumeric(strIP) then
Response.End()
ElseIf Not serRegValidate(strPattern,strIP) then
Response.End()
End If
Dim strURL:strURL="http://www.youdao.com/smartresult-xml/search.s?type=ip&q="
set parser=Server.CreateObject("MSXML2.DOMDocument")
parser.async=false
parser.ValidateOnParse=true
parser.setProperty "ServerHTTPRequest",true
parser.load(strURL)
if parser.parseError.errorCode<>0 then
Response.End()
end if
set currNode=parser.selectNodes("http://product")
Dim strLocal:strLocal=currNode.item(0).selectSingleNode("location").text
Response.Write Split(strLocal," ")(0)
%>
相關(guān)文章
asp之GetArray提取鏈接地址,以$Array$分隔的代碼
asp之GetArray提取鏈接地址,以$Array$分隔的代碼...2007-09-09啟動iis出現(xiàn)發(fā)生意外0x8ffe2740的解決方法
微軟的解釋:如果系統(tǒng)上存在端口沖突,則可能發(fā)生此行為。2009-06-06IIS7.5調(diào)用asp頁面出現(xiàn)800a0e7a的解決辦法
本文給大家分享的是在windows2008R2 64位系統(tǒng)中出現(xiàn)了ADODB.Connection 錯誤 '800a0e7a'的解決辦法,方法很簡單,可是處理過程卻很曲折,這里推薦給大家,有需要的小伙伴可以參考下。2015-05-05