作家
登录

JS跨域代码片段

作者: 来源:www.28hudong.com 2013-03-30 00:23:54 阅读 我要评论

下面的代码块是js调用一般处理程序的代理来实现js跨域的。如果js需要多次跨域,推荐下面的方法。 复制代码 代码如下:public string GetInfo(HttpContext context) { string post = "a=XX&b=XX"; return CreateHttpRequest("https://www.XXXX.com", post, "POST"); } #region 构造请求 /// <summary> /// 构造请求 /// </summary> /// <param name="requestUrl">请求地址</param> /// <param name="requestParam">请求参数</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

  推荐阅读

  推荐17个优美新鲜的jQuery的工具提示插件

在web开发当中,工具提示条对于完善web网站的用户体验至关重要。title属性通常是用来帮助用户了解显示链接的信息。默认情况下用于一个访客来说真是不好找,因为鼠标放上去他才可能显示。如果做一个像电子商务网站的>>>详细阅读


本文标题:JS跨域代码片段

地址:http://www.17bianji.com/kaifa2/JS/23135.html

关键词: 探索发现

乐购科技部分新闻及文章转载自互联网,供读者交流和学习,若有涉及作者版权等问题请及时与我们联系,以便更正、删除或按规定办理。感谢所有提供资讯的网站,欢迎各类媒体与乐购科技进行文章共享合作。

网友点评
自媒体专栏

评论

热度

精彩导读
栏目ID=71的表不存在(操作类型=0)