Solve the "no permission" error in cross-origin access in Ajax.

Source: Internet
Author: User

Prohibit Access to non-domain websites, the following 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 is not found or server 505 error! ");
}
}
}
Obao. Send (null );
}
Getwebpage ('HTTP: // www.google.cn ');
</SCRIPT>

Save this sectionCodeTo test.html, you can use IE to open the code locally. However, after uploading the code to the server, the error occurs-js prompts "no permission !!! How can this be solved?
Now, we can only access the addresses in the same domain. How can we obtain the webpage content from dynamic files in the same domain? We still think of Ajax, but this Ajax is executed on the server side.
The general idea is as follows: first, submit the URL to a file on your site using Ajax, such as getpage. ASP --- in getpage. ASP accesses the submitted URL through the XMLHTTP server again-returns the obtained content to the submitted URL page-display content
Start with the Organization Code. The first step is 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 is not found or server 505 error! ");
}
}
}
Obao. Send (null );
}
Getwebpage ('HTTP: // www.google.cn ');
</SCRIPT>

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

<%
response. charset = "UTF-8"
Reg = "\ \/] *). + \/{0, 1 }\> "
'function name: getresstr
'function: Get the HTML code of the specified URL
' parameter: 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 function

'Function name: bytestobstr
'Function: Convert binary data into characters
'Parameter: 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 into characters
'Parameter: 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 match is found
Set Cols = reg. Execute (STR)
Serstr = Cols (0). submatches (0) 'use the first matched item
Else' otherwise, the default value is gb2312, which is a bit lazy. If the encoding format is not provided on the page, it is a bit troublesome.
Serstr = "gb2312"
End if
Getcode = serstr
End Function

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

The Code Organization is complete, the experiment, successfully extracted the content of the http://www.google.cn !!!!! In this way, the "no permission" problem can be solved.
In fact, it can be obtained just like getpage. asp, but it cannot dynamically process Dom like Js.
There is another problem if you access the http://www.baidu.com using the first method will be garbled because Baidu is encoded as gb2312,
XMLHTTP returns a UTF-8 encoding format. This problem will not occur in the second method. As long as the website with the encoding format is defined, the information will be returned normally (some websites with special encoding cannot be 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.