JS跨域代碼片段
更新時(shí)間:2012年08月30日 12:04:49 作者:
js跨域我用的比較多的就是jsonp和程序代理。但是jsonp只能用get,而且是js異步調(diào)用,有時(shí)候不能滿足項(xiàng)目要求
下面的代碼塊是js調(diào)用一般處理程序的代理來(lái)實(shí)現(xiàn)js跨域的。如果js需要多次跨域,推薦下面的方法。
public string GetInfo(HttpContext context)
{
string post = "a=XX&b=XX";
return CreateHttpRequest("https://www.XXXX.com", post, "POST");
}
#region 構(gòu)造請(qǐng)求
/// <summary>
/// 構(gòu)造請(qǐng)求
/// </summary>
/// <param name="requestUrl">請(qǐng)求地址</param>
/// <param name="requestParam">請(qǐng)求參數(shù)</param>
/// <param name="requestMethod">請(qǐng)求方式</param>
/// <returns></returns>
public string CreateHttpRequest(string requestUrl, string requestParam, string requestMethod)
{
try
{
System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(requestUrl) as System.Net.HttpWebRequest;
request.Method = requestMethod;
string post = requestParam;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(post);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
System.IO.Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
return sr.ReadToEnd();
}
catch (Exception)
{
return "";
}
}
#endregion
復(fù)制代碼 代碼如下:
public string GetInfo(HttpContext context)
{
string post = "a=XX&b=XX";
return CreateHttpRequest("https://www.XXXX.com", post, "POST");
}
#region 構(gòu)造請(qǐng)求
/// <summary>
/// 構(gòu)造請(qǐng)求
/// </summary>
/// <param name="requestUrl">請(qǐng)求地址</param>
/// <param name="requestParam">請(qǐng)求參數(shù)</param>
/// <param name="requestMethod">請(qǐng)求方式</param>
/// <returns></returns>
public string CreateHttpRequest(string requestUrl, string requestParam, string requestMethod)
{
try
{
System.Net.HttpWebRequest request = System.Net.HttpWebRequest.Create(requestUrl) as System.Net.HttpWebRequest;
request.Method = requestMethod;
string post = requestParam;
byte[] bytes = System.Text.Encoding.UTF8.GetBytes(post);
request.ContentType = "application/x-www-form-urlencoded";
request.ContentLength = bytes.Length;
System.IO.Stream stream = request.GetRequestStream();
stream.Write(bytes, 0, bytes.Length);
System.Net.HttpWebResponse response = request.GetResponse() as System.Net.HttpWebResponse;
System.IO.StreamReader sr = new System.IO.StreamReader(response.GetResponseStream(), System.Text.Encoding.UTF8);
return sr.ReadToEnd();
}
catch (Exception)
{
return "";
}
}
#endregion
相關(guān)文章
javascript讀取xml實(shí)現(xiàn)javascript分頁(yè)
這篇文章主要介紹了javascript讀取xml數(shù)據(jù)對(duì)其實(shí)現(xiàn)javascript分頁(yè)效果,大家參考使用吧2013-12-12
input?獲取光標(biāo)位置設(shè)置光標(biāo)位置方案
這篇文章主要為大家介紹了input?獲取光標(biāo)位置設(shè)置光標(biāo)位置方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
cocos2dx骨骼動(dòng)畫Armature源碼剖析(一)
cocos2dx中的骨骼動(dòng)畫在程序中使用非常方便,從編輯器(cocostudio或flash插件dragonBones)得到xml或json數(shù)據(jù),調(diào)用代碼就可以直接展示出動(dòng)畫效果,下面通過(guò)本篇文章給大家分享cocos2dx骨骼動(dòng)畫Armature源碼剖析,需要的朋友一起來(lái)學(xué)習(xí)吧。2015-09-09
小程序安全指南之如何禁止外部直接跳轉(zhuǎn)到小程序某頁(yè)面
由于小程序跳轉(zhuǎn)的對(duì)象比較多,各自的規(guī)則又不一樣,因此小程序跳轉(zhuǎn)外部鏈接是用戶咨詢較多的問(wèn)題之一,下面這篇文章主要給大家介紹了關(guān)于小程序安全指南之如何禁止外部直接跳轉(zhuǎn)到小程序某頁(yè)面的相關(guān)資料,需要的朋友可以參考下2022-09-09
JavaScript 保存數(shù)組到Cookie的代碼
大部分的瀏覽器一個(gè)網(wǎng)站只支持保存20個(gè)Cookie,超過(guò)20個(gè)Cookie,舊的Cookie會(huì)被最新的Cookie代替。那么如果要有超過(guò)20個(gè)Cookie要保存只能將Cookie存為數(shù)組然后保存到Cookie。2010-04-04
JS控制鼠標(biāo)拒絕點(diǎn)擊某一按鈕的實(shí)例
下面小編就為大家分享一篇JS控制鼠標(biāo)拒絕點(diǎn)擊某一按鈕的實(shí)例,具有很好的參考價(jià)值,希望對(duì)大家有所幫助。一起跟隨小編過(guò)來(lái)看看吧2017-12-12
javascript contains和compareDocumentPosition 方法來(lái)確定是否HTML節(jié)點(diǎn)間的關(guān)
一個(gè)很棒的 blog 文章,是 PPK 兩年前寫的,文章中解釋了 contains() 和 compareDocumentPosition() 方法運(yùn)行在他們各自的瀏覽器上。2010-02-02
cordova入門基礎(chǔ)教程及使用中遇到的一些問(wèn)題總結(jié)
這篇文章主要給大家介紹了關(guān)于cordova的入門基礎(chǔ)教程以及在使用中遇到的一些問(wèn)題,文中通過(guò)示例代碼一步步介紹的非常詳細(xì),對(duì)大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來(lái)一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

