Because ajax has a problem with cross-origin access, the best method is to act as a proxy. I wrote a proxy program and my experiences.
In order to act as an ajax proxy, I studied xmlhttp on the lower server and compared it with xmlhttp on the client ajax. The background code is asp
The xmlhttp on the server is the asp thief program. I changed the code to javascript.
1. the xmlhttp. Open ("GET", url, false) Asynchronization on the server must be disabled, while the client Asynchronization is enabled, which is understandable.
2. the xmlhttp. Responsebody on the server uses Responsebody instead of ResponseText or ResponseXml. At first, I used ResponseText, but in the function
An error is prompted when bytesToBSTR is converted. After comparison, it is found that the code in other asp thieves programs is Responsebody. After analysis, it is found that the body returns binary data and
It does not return characters or dom objects like ResponseText or ResponseXml.
Introduction to asp proxy functions of ajax
Send_request (url). The url is the address.
The server-side code is as follows with automatic identification of all character codes. The Japanese, Korean, and traditional Chinese characters have been tested.Copy codeThe Code is as follows: <% @ LANGUAGE = "JAVASCRIPT" CODEPAGE = "65001" %>
<! Doctype html public "-// W3C // dtd html 4.01 Transitional // EN" "http://www.w3.org/TR/html4/loose.dtd">
<Html>
<Head>
<Meta http-equiv = "Content-Type" content = "text/html; charset = UTF-8">
<Title> XMLHTTP </title>
</Head>
<%
Server. ScriptTimeout = 9999999;
Function send_request (url ){
Var codedtext;
Http_request = Server. CreateObject ("Microsoft. XMLHTTP ");
Http_request.Open ("GET", url, false );
Http_request.Send (null );
If (http_request.ReadyState = 4 ){
// Automatically determines the start of Encoding
Var charresult = http_request.ResponseText.match (/CharSet = (\ S +) \ ">/I );
If (charresult! = Null ){
Var Cset = charresult [1];
} Else {Cset = "gb2312"} // gb2312 code is used for websites that cannot be obtained. You can change the code on your own.
// Automatically determines the end Of the Encoding
Codedtext = bytesToBSTR (http_request.Responsebody, Cset );
} Else {
Codedtext = "Erro ";
}
Return (codedtext );
}
Function bytesToBSTR (body, Cset ){
Var objstream;
Objstream = Server. 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;
Return (bytesToBSTR );
}
%>
<Body>
<% Response. Write (send_request ("http://www.daum.net") %>
</Body>
</Html>