前两天,一个朋友让我帮他写这样一个程序:在asp.net里面访问asp的页面,把数据提交对方的数据库后,根据返回的值(返回值为:OK或ERROR),若是为OK再把填入本地数据库。当时,想固然,以为很简单,用js的xmlhttp ,若是根据response 的值是“OK”就执行提交本地数据库。很快写完发过去,让朋友试试,一试发现不行,后来一问,原来是跨域访问,我给忽略了,因而让朋友把asp改为web service,可朋友说程序是合做公司作的,只会asp,不会用web service ,狂晕ing。没办法,只能请出asp.net的 WebResponse了,不少网站采集程序都是用这个。初版写完了,却是能够跨域访问了,不过是乱码,调整有关编码的方式,终于能够了。这个应用虽小但是涉及的知识点很多:php
一、xmlhttp 不能跨域提交。html
固然XMLHttpRequest仍是权宜的解决的方法,jquery
二、webresponse能够进行跨域访问,不过要注意web
1)、get和post的区别。
2)、注意Timeout的问题。ajax
这些都是简单的程序,记下来备忘,高手就没必要看了。数据库
不废话了,下面是相关的c#代码:json
代码以下 | 复制代码 |
/// <summary> /// 使用Post方法发送数据 /// </summary> /// <param name=”pi_strPostURl”>提交地址</param> /// <param name=”pi_strParm”>参数</param> /// <returns></returns> public static string PostResponse(string pi_strPostURl, string pi_strParm) { try { //编码 Encoding t_Encoding = Encoding.GetEncoding(“GB2312“); Uri t_Uri = new Uri(pi_strPostURl); byte[] paramBytes = t_Encoding.GetBytes(pi_strParm); WebRequest t_WebRequest = WebRequest.Create(t_Uri); t_WebRequest.Timeout = 100000; //设置ContentType t_WebRequest.ContentType = “application/x-www-form-urlencoded“; t_WebRequest.Method = EnumMethod.POST.ToString(); //初始化 using (Stream t_REStream = t_WebRequest.GetRequestStream()) { //发送数据 requestStream.Write(paramBytes, 0 , paramBytes.Length); } WebResponse t_WebResponse = t_WebRequest.GetResponse(); using (StreamReader t_StreamReader = new StreamReader(t_WebResponse .GetResponseStream(), t_Encoding)) { return t_StreamReader.ReadToEnd(); } } catch { return “ERROR“; } } public static string GetResponse(string pi_strPostURl, string pi_strParm) { try { //编码 Encoding t_Encoding = Encoding.GetEncoding(“GB2312“); Uri t_Uri = new Uri(string.Format(“{0}?{1}“, pi_strPostURl, pi_strParm)); WebRequest t_WebRequest = WebRequest.Create(t_Uri); t_WebRequest.Timeout = 100000; t_WebRequest.ContentType = “application/x-www-form-urlencoded“; t_WebRequest.Method = EnumMethod.GET.ToString(); WebResponse t_WebResponse = t_WebRequest.GetResponse(); using (StreamReader t_StreamReader = new StreamReader(t_WebResponse.GetResponseStream(), t_Encoding)) { return t_StreamReader.ReadToEnd(); } } catch (Exception e) { return e.ToString(); } } public static string AtionResponse(string pi_Url, EnumMethod pi_Method) { string t_strUrlPath=“”; string t_parm = “”; Uri t_Url = new Uri(pi_Url); t_parm= t_Url.Query; if (parmString.StartsWith(“?“)) t_parm = t_parm.Remove(0, 1); t_strUrlPath = “http://“ + t_Url .Authority + t_Url .AbsolutePath; return GetResponse(t_strUrlPath, t_parm, pi_Method); } public enum EnumMethod { POST, GET } |
如今jquery ajax支持跨域了,下面看个实例咱们可对上面进行处理成json数据便可c#
JQuery.getJSON也一样支持jsonp的数据方式调用。跨域
客户端JQuery.ajax的调用代码示例:app
代码以下 | 复制代码 |
$.ajax({ |
服务端返回数据的示例代码:
代码以下 | 复制代码 |
public void ProcessRequest (HttpContext context) { context.Response.ContentType = "text/plain"; String callbackFunName = context.Request["callbackparam"]; context.Response.Write(callbackFunName + "([ { name:"John"}])"); } |
而jquery.getScript方式处理的原理相似,也一样须要服务端返回数据上作支持,不一样的是服务端返回的结果不一样。不是返回一个callback的function调用,而是直接将结果赋值给请求传递的变量名。客户端则是像引入一个外部script同样加载返回的数据 来源:http://www.111cn.net/net/net/56393.htm。