Solve the Problem of ''no permission'' during 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 code to test.html. It is okay to open it with IE locally. However, after uploading this code to the server, the error occurs. -- JS prompts "no permission" error !!! 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 = "\ <meta. + charset = {0,} ([^ \" "\> \/] *). + \/{0, 1} \>"
'Function name: GetResStr
'Purpose: 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 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.