ajax跨域调用webservice方法实例

webservice代码部分:

   [WebMethod(Description = "这是一个描述")]
        public void GetTIM()
         {
             try
             {
                 SqlDataAdapter da = new SqlDataAdapter("select * from Status", Con);
                 DataSet ds = new DataSet();
                 da.Fill(ds);
                 List<ThingNetwork> Stulis = new List<ThingNetwork>();
                 DataTable dt = ds.Tables[0];

                 foreach (DataRow row in dt.Rows)
                 {
                     ThingNetwork st = new ThingNetwork();
                     st.Ieee = row["Ieee"].ToString();
                     st.CO2 = row["CO2"].ToString();

                     Stulis.Add(st);

                    
                 }
                 string result = JsonConvert.SerializeObject(Stulis);

                 if (!string.IsNullOrEmpty(Context.Request["callback"]))
                 {
                     result = Context.Request["callback"] + "(" + result + ")";
                 }

                 Context.Response.Clear();
                 Context.Response.Charset = "UTF-8";
                 Context.Response.ContentType = "text/plain";
                 Context.Response.Write(result);  //这里是json个文本
                 Context.Response.End();

           
                
             }
             catch (Exception ms)
             {
                 System.Web.UI.Page tt = new System.Web.UI.Page();
                 tt.Response.Write(ms.Message);
                
             }
             finally
             {
                 Con.Close();
             }
            
            
        }
       
config配置代码:

  <system.web>
        <compilation debug="true" >

        </compilation>
    <!--
      通过 <authentication> 节,可配置 
      ASP.NET 用于识别进入用户的 
      安全身份验证模式。
    -->
    <authentication mode="Windows" />
    <!--
       通过 <customErrors> 节,可以配置
       在执行请求的过程中出现未处理的错误时要执行 
       的操作。具体而言,
       开发人员通过该节可配置要显示的 html 错误页,
       以代替错误堆栈跟踪。

       <customErrors mode="RemoteOnly" defaultRedirect="GenericErrorPage.htm">
         <error statusCode="403" redirect="NoAccess.htm" />
         <error statusCode="404" redirect="FileNotFound.htm" />
       </customErrors>
    -->
      <webServices>
        <protocols>
          <add name="HttpGet" />      ------这里一定要配置
          <add name="HttpPost" />
          <add name="HttpSoap" />
        </protocols>
      </webServices>
    </system.web>

前端代码部分:

 $.ajax({          
            url: 'http://localhost:1756/WebService.asmx/GetTIM?callback=?',
            dataType: 'jsonp',
            data: {},
            jsonp: 'jsoncallback',
            contentType: 'application/json; charset=utf-8',
            success: function (data) {
                $.each(data, function (i, n) {
                    alert(n.Ieee);
                });
                
            },
            error: function () {
                alert('错误!');
            }
        });

这样就OK啦!!!


评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值