JS跨域代碼片段
更新時(shí)間:2012年08月30日 12:04:49 作者:
js跨域我用的比較多的就是jsonp和程序代理。但是jsonp只能用get,而且是js異步調(diào)用,有時(shí)候不能滿足項(xiàng)目要求
下面的代碼塊是js調(diào)用一般處理程序的代理來實(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)造請求
/// <summary>
/// 構(gòu)造請求
/// </summary>
/// <param name="requestUrl">請求地址</param>
/// <param name="requestParam">請求參數(shù)</param>
/// <param name="requestMethod">請求方式</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)造請求
/// <summary>
/// 構(gòu)造請求
/// </summary>
/// <param name="requestUrl">請求地址</param>
/// <param name="requestParam">請求參數(shù)</param>
/// <param name="requestMethod">請求方式</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分頁
這篇文章主要介紹了javascript讀取xml數(shù)據(jù)對其實(shí)現(xiàn)javascript分頁效果,大家參考使用吧2013-12-12
input?獲取光標(biāo)位置設(shè)置光標(biāo)位置方案
這篇文章主要為大家介紹了input?獲取光標(biāo)位置設(shè)置光標(biāo)位置方案詳解,有需要的朋友可以借鑒參考下,希望能夠有所幫助,祝大家多多進(jìn)步,早日升職加薪2023-06-06
小程序安全指南之如何禁止外部直接跳轉(zhuǎn)到小程序某頁面
由于小程序跳轉(zhuǎn)的對象比較多,各自的規(guī)則又不一樣,因此小程序跳轉(zhuǎn)外部鏈接是用戶咨詢較多的問題之一,下面這篇文章主要給大家介紹了關(guān)于小程序安全指南之如何禁止外部直接跳轉(zhuǎn)到小程序某頁面的相關(guān)資料,需要的朋友可以參考下2022-09-09
JavaScript 保存數(shù)組到Cookie的代碼
大部分的瀏覽器一個網(wǎng)站只支持保存20個Cookie,超過20個Cookie,舊的Cookie會被最新的Cookie代替。那么如果要有超過20個Cookie要保存只能將Cookie存為數(shù)組然后保存到Cookie。2010-04-04
JS控制鼠標(biāo)拒絕點(diǎn)擊某一按鈕的實(shí)例
下面小編就為大家分享一篇JS控制鼠標(biāo)拒絕點(diǎn)擊某一按鈕的實(shí)例,具有很好的參考價(jià)值,希望對大家有所幫助。一起跟隨小編過來看看吧2017-12-12
javascript contains和compareDocumentPosition 方法來確定是否HTML節(jié)點(diǎn)間的關(guān)
一個很棒的 blog 文章,是 PPK 兩年前寫的,文章中解釋了 contains() 和 compareDocumentPosition() 方法運(yùn)行在他們各自的瀏覽器上。2010-02-02
cordova入門基礎(chǔ)教程及使用中遇到的一些問題總結(jié)
這篇文章主要給大家介紹了關(guān)于cordova的入門基礎(chǔ)教程以及在使用中遇到的一些問題,文中通過示例代碼一步步介紹的非常詳細(xì),對大家的學(xué)習(xí)或者工作具有一定的參考學(xué)習(xí)價(jià)值,需要的朋友們下面隨著小編來一起學(xué)習(xí)學(xué)習(xí)吧。2017-11-11

