Resolve "No Permissions" error _javascript techniques for Cross-domain access in Ajax

Source: Internet
Author: User
Prohibit access to a site that is not in the same domain, an example below 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 ("Not found the address you entered or server 505 error!");
}
}
}
Obao.send (NULL);
}
Getwebpage (' http://www.google.cn ');
</script>

Save this code to test.html, the local directly with IE open no problem, but the code uploaded to the server, the problem arises--js prompted "No Permissions" error!!! How should this be solved?
Consider below: Since can not access the domain, can only access the same domain address, the same domain dynamic files how to get the content of the Web page is not the same domain? We still think of Ajax, except that this Ajax is performed on the server side.
The general idea is this: first the URL with Ajax submitted to their own station files, such as getpage.asp---in getpage.asp again through the server XMLHTTP to access the submitted URL---will get the content returned to the submission URL of the page----display content
The following starts with the organization code, first 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 ("Not found the address you entered or server 505 error!");
}
}
}
Obao.send (NULL);
}
Getwebpage (' http://www.google.cn ');
</script>

And then getpage.asp file (Note: To save this file in UTF-8 format, to prevent garbled), as follows:


<%
response.charset= "UTF-8"
Reg= "\<meta.+ charset= {0,}" ([^\ "\>\/]*) .+\/{0,1}\>"
' Function name: getresstr
' function: Gets the HTML code for the specified URL
' Parameter: url-the URL to get
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-to query string, 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 query to match
Set Cols = Reg.execute (str)
Serstr=cols (0). Submatches (0) ' uses the first match to match
Else ' Otherwise give a default value of gb2312, a bit lazy, if the page does not give the code format, want to know that there is a bit of trouble
Serstr= "gb2312"
End If
Getcode=serstr
End Function

Dim url:url=request.querystring ("url")
Response.Write Getresstr (URL)
%>

The code is organized and the content of http://www.google.cn is extracted successfully under the experiment!!!!! This will solve the problem of "No permissions".
In fact, a simple getpage.asp like to get, but can not be as dynamic as JS processing dom.
There is also a problem, if you use the first method to access http://www.baidu.com will appear garbled, because the Baidu code for GB2312,
Instead, XMLHTTP returns the UTF-8 encoding format. This is not the case with the second method, as long as the site in which the coded format is defined can return the information normally (some sites that use special code are not included here).
Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.