使用webservice后台跨域访问获取数据
//处理内容 string html = ""; string url = ""; HttpWebRequest request = (HttpWebRequest)WebRequest.Create(url); request.Accept = "*/*"; //接受任意文件 request.AllowAutoRedirect = true;//是否允许302 //request.CookieContainer = new CookieContainer();//cookie容器, request.Referer = url; //当前页面的引用 HttpWebResponse response = (HttpWebResponse)request.GetResponse(); Stream stream = response.GetResponseStream(); StreamReader reader = new StreamReader(stream, Encoding.UTF8); html = reader.ReadToEnd(); stream.Close(); Context.Response.Charset = "UTF-8"; Context.Response.ContentEncoding = System.Text.Encoding.GetEncoding("UTF-8"); Context.Response.Write(html); Context.Response.End();
前台ajax请求
<script> $(function () { $("#getData").click(function () { $.post("http://localhost:2279/WebService1.asmx/GetData", {}, function (data) { }, "json"); }); }) </script>
时间: 2024-10-14 02:03:16