ajax跨域调用webservice例子

  1. [WebMethod(Description = "这是一个描述")]  
  2.      public void GetTIM()  
  3.       {  
  4.           try  
  5.           {  
  6.               SqlDataAdapter da = new SqlDataAdapter("select * from Status", Con);  
  7.               DataSet ds = new DataSet();  
  8.               da.Fill(ds);  
  9.               List<ThingNetwork> Stulis = new List<ThingNetwork>();  
  10.               DataTable dt = ds.Tables[0];  
  11.   
  12.               foreach (DataRow row in dt.Rows)  
  13.               {  
  14.                   ThingNetwork st = new ThingNetwork();  
  15.                   st.Ieee = row["Ieee"].ToString();  
  16.                   st.CO2 = row["CO2"].ToString();  
  17.   
  18.                   Stulis.Add(st);  
  19.   
  20.                    
  21.               }  
  22.               string result = JsonConvert.SerializeObject(Stulis);  
  23.   
  24.               if (!string.IsNullOrEmpty(Context.Request["callback"]))  
  25.               {  
  26.                   result = Context.Request["callback"] + "(" + result + ")";  
  27.               }  
  28.   
  29.               Context.Response.Clear();  
  30.               Context.Response.Charset = "UTF-8";  
  31.               Context.Response.ContentType = "text/plain";  
  32.               Context.Response.Write(result);  //这里是json个文本  
  33.               Context.Response.End();  
  34.   
  35.           
  36.                
  37.           }  
  38.           catch (Exception ms)  
  39.           {  
  40.               System.Web.UI.Page tt = new System.Web.UI.Page();  
  41.               tt.Response.Write(ms.Message);  
  42.                
  43.           }  
  44.           finally  
  45.           {  
  46.               Con.Close();  
  47.           }  
  48.            
  49.            
  50.      }  
  1. <system.web>  
  2.       <compilation debug="true" >  
  3.   
  4.       </compilation>  
  5.   <!--  
  6.     通过 <authentication> 节,可配置   
  7.     ASP.NET 用于识别进入用户的   
  8.     安全身份验证模式。  
  9.   -->  
  10.   <authentication mode="Windows" />  
  11.   <!--  
  12.      通过 <customErrors> 节,可以配置  
  13.      在执行请求的过程中出现未处理的错误时要执行   
  14.      的操作。具体而言,  
  15.      开发人员通过该节可配置要显示的 html 错误页,  
  16.      以代替错误堆栈跟踪。  
  17.   
  18.      <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">  
  19.        <error statusCode="403" redirect="NoAccess.htm" />  
  20.        <error statusCode="404" redirect="FileNotFound.htm" />  
  21.      </customErrors>  
  22.   -->  
  23.     <webServices>  
  24.       <protocols>  
  25.         <add name="HttpGet" />      ------<span style="color:#ff0000;">这里一定要配置</span>  
  26.         <add name="HttpPost" />  
  27.         <add name="HttpSoap" />  
  28.       </protocols>  
  29.     </webServices>  
  30.   </system.web>  
  1. $.ajax({            
  2.            url: 'http://localhost:1756/WebService.asmx/GetTIM?callback=?',  
  3.            dataType: 'jsonp',  
  4.            data: {},  
  5.            jsonp: 'jsoncallback',  
  6.            contentType: 'application/json; charset=utf-8',  
  7.            success: function (data) {  
  8.                $.each(data, function (i, n) {  
  9.                    alert(n.Ieee);  
  10.                });  
  11.                  
  12.            },  
  13.            error: function () {  
  14.                alert('错误!');  
  15.            }  
  16.        });  

ok

转自:http://blog.csdn.net/meiminjun2012/article/details/17304651

原文地址:https://www.cnblogs.com/sdya/p/7886481.html