ajax跨域访问远程webservice、ashx的解决方法
1.HTML页面,主要是公文流转的webservice跨域,内外网邮箱的ashx跨域(用$.getJSON解决)
//公文流转 $(function(){ $.ajax({ type:"POST", url:"wsOADoc.asmx/GetDocCount", data:{"utName":"linshan"}, success: function(strNum) { var xmlMsg = strNum.getElementsByTagName("int"); var txt = xmlMsg[0].childNodes[0].nodeValue; $("#strCount").html(txt); } }); }); //内网邮箱 $(function(){ var url = 'http://10.72.8.235/Mail.ashx'; $.getJSON(url+'?callback=?',{userName:'linshan'}, function(result){ $("#InnerMail").html(result.UnReadMailCount); } ) }) //外网邮箱 $(function(){ var url = 'http://10.72.8.235/wideMail.ashx'; $.getJSON(url+'?callback=?',{userName:'linshan'}, function(result){ $("#wideMail").html(result.UnReadMailCount); } ) })
2.跨域webservice解决:在本地建立一个代理,用于转换
using System; using System.Web; using System.Collections; using System.Web.Services; using System.Web.Services.Protocols; /// <summary> /// wsOADoc 的摘要说明 /// </summary> [WebService(Namespace = "http://tempuri.org/")] [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)] public class wsOADoc : System.Web.Services.WebService { public wsOADoc () { //如果使用设计的组件,请取消注释以下行 //InitializeComponent(); } [WebMethod] public string HelloWorld() { return "Hello World"; } [WebMethod] public int GetDocCount(string utName) { DocuOA.TodayAffair ta = new DocuOA.TodayAffair(); int strNum = Convert.ToInt32(ta.utGetTodayDocuOA3Count(utName)); if (strNum >= 0) { return strNum; } else { return -1; } } }