Ajax クロスドメイン アクセスで権限がないと報告される問題を解決します。困っている友達は参考にしてください。スマート.asp
次のようにコードをコピーします。
<スクリプト言語=jscript runat=サーバー>
/*
VBS でのメソッドの呼び出し
私のhttpを暗くします
set myhttp = SmartHttp(url,method,data); //3 つのパラメータはすべてオプションです
財産:
url: 文字列、要求された URL アドレス
メソッド: 文字列、要求されたメソッド
データ: 文字列、要求されたデータ
charset: 文字列、リクエストされた URL によって返されるデータのエンコード
status: Int、リクエストによって返されるステータス コード
ReadyState: Int、HTTP リクエストとの現在の通信ステータス、1、2、3、4
dataset: オブジェクト、要求されたデータ。追加された場合、データのこの部分がデータ属性に追加されます。
データセットのプロパティ:
charset: 文字列、送信データのエンコーディング
データセットメソッド:
append(key,value,noencode): データを追加します
Remove(key): データ項目を削除します
isexists(key): データ項目が存在するかどうかを判断します。
Clear: すべてのデータ項目をクリアします
方法:
header(headstr): リクエストヘッダーを設定し、項目と値を次のように区切ります。
timeout(t1,t2,t3,t4): タイムアウト時間を設定します
send(): リクエストの送信
getbinary: サーバーから返されたバイナリデータを取得します
gettext(charset): 指定されたエンコーディングのテキストを取得します。
getjson(charset): 指定したエンコーディングのjsonデータを取得します。
getheader(key): サーバーから返された応答ヘッダーを取得します。
getxml(charset): 指定されたエンコーディングの XML データを取得します
*/
関数 SmartHttp(url,メソッド,データ){
新しい _SmartHttp(url,メソッド,データ) を返します;
}
関数 _SmartHttp(url,メソッド,データ){
if(メソッドの種類==未定義) メソッド=GET;
if(データの種類==未定義) データ=;
メソッド = メソッド.toUpperCase();
メソッド = メソッド!=POST : POST;
this.timeout=[10000,10000,10000,10000];
this.method = メソッド;
this.url=url;
this.data=データ;
this.charset=gb2312;
this.http=null;
this.headers=[];
this.status=0;
this.readyState=0;
this.content=null;
this.msg=;
this.dataset={
文字セット:gb2312、
データ:[]、
append:function(key,value,noencode){
var fn=null;
if(this.charset.toLowerCase()==utf-8){fn = encodeURIComponent;}else{fn =エスケープ;}
if(noencode==true){fn=function(_str){return _str;}}
this.data.push({key:fn(key),value:fn(value)});
}、
削除:関数(キー){
if(this.data.length<=0) は false を返します。
var_data=[];
for(var i=0;i<this.data.length;i++){
if(this.data[i].key!=key){
_data.push(this.data[i]);
}
}
this.data = _data;
}、
isexists:関数(キー){
if(this.data.length<=0) は false を返します。
for(var i=0;i<this.data.length;i++){
if(this.data[i].key==key){
true を返します。
}
}
false を返します。
}、
クリア:function(){
this.dataset.data=[];
}
};
}
_SmartHttp.prototype.init=function(){
var datasetstr=;
if(this.dataset.data.length>0){
for(var i=0;i<this.dataset.data.length;i++){
datasetstr += this.dataset.data[i].key + = + this.dataset.data[i].value + &;
}
}
if(datasetstr!=) datasetstr = datasetstr.substr(0,datasetstr.length-1);
if(this.data==){this.data = datasetstr;}else{if(datasetstr!=)this.data+= & + datasetstr;}
if(this.data==)this.data=null;
//this.url += ((this.url.indexOf(?)<0) ? ? : &) + jornd= + this.getrnd();
if(this.method==GET && this.data!=null) this.url += & + this.data;
if(this.method==POST) this.headers.push(Content-Type:application/x-www-form-urlencoded);
if(!this.charset || this.charset==) this.charset = gb2312;
};
_SmartHttp.prototype.header=function(headstr){
if(headstr.indexOf(:)>=0) this.headers.push(headstr);
これを返します。
};
_SmartHttp.prototype.timeout=function(){
if(arguments.length>4){これを返す;}
for(var i =0;i<arguments.length;i++){
if(!isNaN(引数[i])){
this.timeout[i] = parseInt(arguments[i]);
}
}
これを返します。
};
_SmartHttp.prototype.send=function(){
this.init();
var _http = this.getobj();
if(_http==null){これを返す;}
試す{
_http.setTimeouts(this.timeout[0], this.timeout[1], this.timeout[2], this.timeout[3]);
}キャッチ(元){}
_http.open(this.method,this.url,false);
if(this.headers.length>0){
for(var i=0;i<this.headers.length;i++){
var Sindex = this.headers[i].indexOf(:);
var key = this.headers[i].substr(0,Sindex);
var value = this.headers[i].substr(Sindex+1);
_http.setRequestHeader(キー,値);
}
}
_http.send(this.data);
this.readyState = _http.readyState;
if(_http.readyState==4){
this.status = parseInt(_http.status);
this.http = _http;
this.content = _http.responseBody;
}
これを返します。
}
_SmartHttp.prototype.getbinary=function(){
this.content を返します。
};
_SmartHttp.prototype.gettext=function(charset){
試す{
return this.b2s(this.content,charset ? charset : this.charset);
}キャッチ(元){
this.msg = ex.description;
戻る ;
}
};
_SmartHttp.prototype.getjson=関数(文字セット){
試す{
var _json=null;
eval(_json=( + this.gettext(charset) + ););
_json を返す;
}キャッチ(元){
this.msg = ex.description;
null を返します。
}
};
_SmartHttp.prototype.getheader=関数(キー){
if(キー){
if(key.toUpperCase()==SET-COOKIE){
key = key.replace(-,/-);
var headers = this.http.getAllResponseHeaders();
var regexp = new RegExp(/n + key + /:(.+?)/r,ig);
var restr = ;
while((res = regexp.exec(headers))!=null){
var val = res[1].trim();
resstr = resstr + val.substr(0,val.indexOf(;)) + ;
}
if(ressstr!=){
resstr = resstr.substr(0,resstr.lastIndexOf(;));
}
resstr を返します。
}それ以外{
this.http.getResponseHeader(key) を返します。
}
}else{これを返す.http.getAllResponseHeaders();}
};
_SmartHttp.prototype.getxml=function(charset){
試す{
var _dom = 新しい ActiveXObject(MSXML2.DOMDocument);
_dom.loadXML(this.gettext(charset));
_dom を返す;
}キャッチ(元){
this.msg = ex.description;
null を返します。
}
};
_SmartHttp.prototype.getobj = 関数 (){
var b=null;
var httplist = [MSXML2.serverXMLHttp.3.0,MSXML2.serverXMLHttp,MSXML2.XMLHttp.3.0,MSXML2.XMLHttp,Microsoft.XMLHttp];
for(var i = 0;i<=httplist.length -1;i++){
試す{
b= 新しい ActiveXObject(httplist[i]);
(関数(o){
_SmartHttp.prototype.getobj = function(){return new ActiveXObject(o)};
})(httplist[i]);
bを返します。
}キャッチ(元){
eval(this.msg = ex.description;);
}
}
bを返します。
};
_SmartHttp.prototype.getrnd = function (){return Math.random().toString().substr(2);};
_SmartHttp.prototype.b2s = function(bytSource, Cset){ //ef bb bf,c0 fd
var Objstream,c1,c2,c3;
var バイト;
Objstream =Server.CreateObject(ADODB.Stream);
Objstream.Type = 1;
Objstream.Mode = 3;
Objstream.Open();
Objstream.Write(bytSource);
Objstream.Position = 0;
Objstream.Type = 2;
Objstream.CharSet = Cset;
byts = Objstream.ReadText();
Objstream.Close();
オブジェクトストリーム = null;
バイトを返します。
};
_SmartHttp.prototype.urlencode=function(str){ return encodeURIComponent(str);};
_SmartHttp.prototype.urldecode=function(str){ return decodeURIComponent(str);};
String.prototype.trim = function(){return this.replace(/(^(/s+)|(/s+)$)/igm,);};
</script>
コードを使用します:
次のようにコードをコピーします。
<!--#include file=smart.asp-->
<%
応答.charset=utf-8
dim URL、メソッド、データ、文字セット
URL =リクエスト.フォーム(ターゲットURL)
メソッド =リクエスト.フォーム(メソッド)
データ =リクエスト.フォーム(データ)
charset = リクエスト.フォーム(charset)
文字セット = の場合、文字セット = GB2312
response.Write SmartHttp(url,method,data).send().gettext(charset)
myhttp = 何も設定しない
%>