ajax跨域解决IE警告和FireFox取不出内容的问题。

 今天测试AJAX,因为我需要从别人的网站“偷”点内容,然后遇到了跨域的问题。IE里面只会有一个提示,内容大概是:你现在要准备跨域了,这个操作可能不安全,要不要继续?点确定就能继续取到内容了。但是FF不行,FF在跨域的时候应该是禁止了吧,取回的内容一直是一段空气。

百度了一下“firefox ajax跨域”,大概看了一下有关firefox里面ajax跨域的问题,具体在这里:http://www.blogjava.net/nokiaguy/archive/2008/05/15/200678.html

文章里面说到两个处理方法:

1、设置Firefox的运行参数,让它允许跨域:about:config

2、用JS命令打到Firefox的设置参数,使其允许跨域:netscape.security.PrivilegeManager.enablePrivilege("UniversalBrowserRead"); 

  

另一文章里还提到,可以用java的网络操作,来实现,其原理如下:

 

//建立一个连接,URLConnection
URL url=new URL("http://www.xxxx.com/aaa.jsp");
conn = url.openConnection();
//调用setDoOutput方法,建立输出连接
conn.setDoOutput(true);
//然后调用getOutputStream方法,以便获得数据流。这个属于java特色了,什么输入输出的玩艺它都当作流。
//一般会有参数,用PrintWriter。
PrintWriter out=new PrintWriter(conn.getOutputStream());
out.print("para1=L¶2=duoduo");//这行是参数
//最后关闭连接就Ok了,
out.close();

因为本人不太懂JAVA,所以只有放弃了。哎,郁闷。

后来想了一下,其实ASP就有一个XMLHTTP对象,用asp先获得目标地址的值,然后再利用AJAX读取此ASP文件,把内容提交到所需的页面,就不存在跨域问题了。PHP的CURL,也一样的。但我不记得fopen是否有post功能了。

我现在的需要就是:我需要从我的网站,POST提交到http://lietu.com/demo/KeyWords.jsp然后取得其POST后的页面内容。
测试
具体点说就是,填好标题以后,点击获取,然后从http://lietu.com/demo/KeyWords.jsp得到关键字,再填充到关键词里面。
方法如下:

1、先建个HTM文件,
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
<title>无标题文档</title>
</head>

<body>
标题:<input type="text" id="title" value="这里我是标题,测试的标题,不是你的标题" size="50" /><br />
关键词:<input type="text" id="kw" size="50" /><input type="button" id="gogo" value="获取" />
<div id="tdiv"></div>
<script type="text/javascript">
$('gogo').onclick=function(){
 var kw=$('kw');
 var ti=$('title')
 var data=(ti?ti.value:'');
 var u='KeyWords.asp'+'?'+Math.random();//刷新
 kw.value='正在提取,请稍候...';
 Ajax({u:u,m:'post',d:'doc1='+encodeURI(data),e:function(s_){
  var Lstr='关键词:<br>',Rstr='</FONT><br>';
  var strL=s_.indexOf(Lstr)+Lstr.length;
  var strR=strL+s_.substr(strL).indexOf(Rstr);
  s_=s_.substr(strL,strR-strL);
  s_=s_.replace(/\:\d+\.\d+\<br\>/g,'\v'=='v'?' ':'');
  kw.value=s_;
 }});
}
function $(o){return document.getElementById(o);} //根据ID取得对象
function Ajax(o){ //o的全貌:{m:'post',u:'sub.asp',d:'c=123',e:function(){}}
 var m,u,d,e,x; //method,url,end,xmlobj
 if(!o.u)return false;
 m=(o.m?o.m.toUpperCase():'GET');
 e=(o.e?o.e:function(){});
 d=(o.d?o.d:'');
 var x=(window.XMLHttpRequest?new XMLHttpRequest():new ActiveXObject("Microsoft.XMLHTTP"));
 x.onreadystatechange=function(){
  if (x.readyState==4 && x.status==200){
   e(x.responseText);
  }
 };
 x.open(o.m,o.u+(o.u.indexOf('?')>0?'&':'?')+Math.random(),true);
 if(m=='POST'){x.setRequestHeader("Content-type","application/x-www-form-urlencoded");}
 x.send(d);
}
</script>
</body>
</html>

随便起个名字,test.html,保存。

2、keywords.asp文件:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="Content-Type" content="text/html; charset=gb2312" />
</head>

<body>
<%
data=request.form()
body=getbody("http://lietu.com/demo/KeyWords.jsp","post",data,"utf-8")
body=cutstr(body,"<body>","</body>")
response.write body
%>
</body>
</html><% 'asp XMLHTTP
Function BytesToBstr(body,Cset)
 dim objstream
 set objstream = Server.CreateObject("adodb.stream")
 with objstream
  .Type = 1
  .Mode =3
  .Open
  .Write body
  .Position = 0
  .Type = 2
  .Charset = Cset
   BytesToBstr= .ReadText
  .Close
 end with
 set objstream = nothing
End Function
Function GetBody(url,method,data,cset)
  if method="" then method="get"
  if cset="" then cset="GB2312"
  method=lcase(method)
        on error resume next
        Set Retrieval = CreateObject("Microsoft.XMLHTTP")
        With Retrieval
        .Open "Get", url, False, "", ""
  if method="post" then
   .setRequestHeader "Content-Type", "application/x-www-form-urlencoded"
         .Send(data)
  else
   .Send
  end if
        GetBody = BytesToBstr(.ResponseBody,cset)
        End With
        Set Retrieval = Nothing
End Function

function cutstr(a_,b_,c_)
 L_=instr(a_,b_)+len(b_)
 R_=instr(L_,a_,c_)
 if R_<L_ then R_=len(a_)
 cutstr=mid(a_,L_,abs(R_-L_))
end function %>


这样,IE里面就没有警告提示,FF也可以取到内容了。 
通过IE7/8,FF3.66测试。其它浏览器没测试。

posted on 2011-06-03 16:19  兔@.@子  阅读(1200)  评论(1)    收藏  举报

导航