zoukankan      html  css  js  c++  java
  • JS跨域代码片段

    js跨域我用的比较多的就是jsonp和程序代理。

    但是jsonp只能用get,而且是js异步调用,有时候不能满足项目要求。

    下面的代码块是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

      

  • 相关阅读:
    UVA 129 Krypton Factor (困难的串)(回溯法)
    UVA 524 Prime Ring Problem(素数环)(回溯法)
    【POJ 2559】Largest Rectangle in a Histogram【栈】
    【POJ 2559】Largest Rectangle in a Histogram【栈】
    向右看齐【栈】
    向右看齐【栈】
    向右看齐【栈】
    【模板】最近公共祖先【LCA】
    【模板】最近公共祖先【LCA】
    【模板】最近公共祖先【LCA】
  • 原文地址:https://www.cnblogs.com/qixuejia/p/2662270.html
Copyright © 2011-2022 走看看