When many people use AJAX to call content on other people's sites, JS will prompt a no permission error. This is a limitation of the XMLHTTP component - for security reasons
It is forbidden to access websites that are not of the same domain. Here is an example to access http://www.google.cn,
<script type=text/javascript>
function createobj() {
if (window.ActiveXObject) {
return new ActiveXObject(Microsoft.XMLHTTP);
}
else if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
}
function getWebPage(url) {
var oBao=createobj();
var my_url=url
oBao.open('get',my_url,false);
oBao.onreadystatechange=function(){
if(oBao.readyState==4){
if(oBao.status==200){
var returnStr=oBao.responseText;
document.write(returnStr);
}else{
document.write(The address you entered was not found or server 505 error!);
}
}
}
oBao.send(null);
}
getWebPage('http://www.google.cn');
</script>
Save this code to test.html and open it locally with IE without any problem, but after uploading this code to the server, a problem occurs - JS prompts no permission error!!! How to solve this?
Let's think about it: Since you can't access non-same domain addresses, you can only access addresses in the same domain. How can dynamic files in the same domain obtain the content of web pages in non-same domain? We still think of AJAX, but this AJAX is executed on the server side.
The general idea is this: first submit the URL to a file in your own site using AJAX, such as getPage.asp---in getPage.asp, access the submitted URL again through server XMLHTTP---return the obtained content to the submitted URL Page----display content
Let’s start organizing the code, starting with the test.html file
<script type=text/javascript>
function createobj() {
if (window.ActiveXObject) {
return new ActiveXObject(Microsoft.XMLHTTP);
}
else if (window.XMLHttpRequest) {
return new XMLHttpRequest();
}
}
function getWebPage(url) {
var oBao=createobj();
var my_url=getpage.asp?url=+escape(url);
oBao.open('get',my_url,false);
oBao.onreadystatechange=function(){
if(oBao.readyState==4){
if(oBao.status==200){
var returnStr=oBao.responseText;
document.write(returnStr);
}else{
document.write(The address you entered was not found or server 505 error!);
}
}
}
oBao.send(null);
}
getWebPage('http://www.google.cn');
</script>
Then there is the getpage.asp file (note: this file must be saved in UTF-8 format to prevent garbled characters), as follows:
<%
response.charset=UTF-8
reg=/<meta.+ charset= {0,}([^/ />//]*).+//{0,1}/>
'Function name: GetResStr
'Function: Get the HTML code of the specified URL
'Parameters: URL-the URL to be obtained
function GetResStr(URL)
err.clear
dim ResBody,ResStr,PageCode,ReturnStr
Set Http=createobject(MiCROSOFT.XMLHTTP)
Http.open GET,URL,False
Http.Send()
If Http.Readystate =4 Then
If Http.status=200 Then
ResStr=http.responseText
ResBody=http.responseBody
PageCode=GetCode(ResStr,reg)
ReturnStr=BytesToBstr(http.responseBody,PageCode)
GetResStr=ReturnStr
End If
End If
End Function
'Function name:BytesToBstr
'Function: Convert binary data to characters
'Parameters: Body-binary data, Cset-text encoding method
Function BytesToBstr(Body,Cset)
Dim Objstream
Set Objstream = CreateObject(adodb.stream)
objstream.Type = 1
objstream.Mode =3
objstream.Open
objstream.Write body
objstream.Position = 0
objstream.Type = 2
objstream.Charset =Cset
BytesToBstr = objstream.ReadText
objstream.Close
set objstream = nothing
End Function
'Function name: GetCode
'Function: Convert binary to character
'Parameters: str-string to be queried, regstr-regular expression
Function GetCode(str,regstr)
Dim Reg,serStr
set Reg= new RegExp
Reg.IgnoreCase = True
Reg.MultiLine = True
Reg.Pattern =regstr
if Reg.test(str) then 'If a matching item is found
Set Cols = Reg.Execute(str)
serStr=Cols(0).SubMatches(0) 'Use the first match found
else 'Otherwise, give the default value gb2312, which is a bit lazy. If the page does not give the encoding format, it is indeed a bit troublesome to know.
serStr=gb2312
end if
GetCode=serStr
end function
dim url:url=request.querystring(url)
response.write GetResStr(URL)
%>
The code has been organized. After experimenting, the content of http://www.google.cn was successfully extracted!!!!! This solves the problem of no permissions.
In fact, a simple getpage.asp can be obtained, but it cannot dynamically process the DOM like js.
There is another problem. If you use the first method to access http://www.baidu.com, garbled characters will appear because baidu encoding is GB2312.
XMLHTTP returns UTF-8 encoding format. Using the second method, such a problem will not occur. As long as the site that defines the encoding format can return information normally (this does not include some sites that use special encoding).