Home  >  Article  >  Web Front-end  >  Solve the 'no permission' error in cross-domain access in AJAX_javascript skills

Solve the 'no permission' error in cross-domain access in AJAX_javascript skills

WBOY
WBOYOriginal
2016-05-16 19:01:391309browse

Access to non-same domain websites is prohibited. Here is an example to access http://www.google.cn,




Save this code to test.html, directly locally There is no problem opening it with IE, but after uploading the code to the server, a problem occurs - JS prompts a "no permission" error!!! How to solve this?
Think about it below: Since you can’t access non-same-domain addresses, you can only access addresses in the same domain. How do you get the content of non-same-domain web pages 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 this: first submit the URL to the file in your own site using AJAX, such as getPage.asp---pass it again in getPage.asp The server XMLHTTP accesses the submitted URL---returns the obtained content to the page where the URL was submitted--displays the content
Let’s start organizing the code, starting with the test.html file




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="
/]*). /{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 match is found
Set Cols = Reg.Execute(str)
serStr=Cols(0).SubMatches(0) 'Use the first matched item
else 'Otherwise give the default value gb2312, which is a bit economical Lazy method, if the page does not give the encoding format, it is really troublesome to know
serStr="gb2312"
end if
GetCode=serStr
end function

dim url:url =request.querystring("url")
response.write GetResStr(URL)
%>

The code is organized. After the experiment, the content of http://www.google.cn was successfully extracted. !!!!This can solve the "no permissions" problem.
Actually, 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, and 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).

Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn