如何通过ajax / json调用webservice [英] How to call webservice through ajax/json

查看:80
本文介绍了如何通过ajax / json调用webservice的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

网络服务代码:







webservice code:



[WebMethod]
    [ScriptMethod(UseHttpGet = false, ResponseFormat = ResponseFormat.Json)]
    public void GetDatBooth()
    {

        HttpContext.Current.Response.AddHeader("Access-Control-Allow-Origin", "*");

        if (HttpContext.Current.Request.HttpMethod == "OPTIONS")
        {
            //These headers are handling the "pre-flight" OPTIONS call sent by the browser
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Methods", "GET, POST, PUT, DELETE");
            HttpContext.Current.Response.AddHeader("Access-Control-Allow-Headers", "Content-Type, Accept");
            HttpContext.Current.Response.AddHeader("Access-Control-Max-Age", "1728000");
            HttpContext.Current.Response.End();
        }
        String resultJSON = "";
        JavaScriptSerializer js = new JavaScriptSerializer();

        try
        {
            Context.Response.Clear();
            Context.Response.ContentType = "application/json";

            JavaScriptSerializer serializer = new JavaScriptSerializer();
            List<Dictionary<String, Object>> tableRow = new List<Dictionary<string, object>>();
            Dictionary<String, Object> rows;
            DataTable dt = new DataTable();
            dt.TableName = "Mytab";
            dt.Columns.Add("BoothNo");
            dt.Columns.Add("VotingQue");
            dt.Columns.Add("Percentage");

            DataRow dr = dt.NewRow();
            dr["BoothNo"] = "01";
            dr["VotingQue"] = "14";
            dr["Percentage"] = 14;

            DataRow dr1 = dt.NewRow();
            dr1["BoothNo"] = "02";
            dr1["VotingQue"] = "12";
            dr1["Percentage"] = 12;

            dt.Rows.Add(dr);
            dt.Rows.Add(dr1);


            foreach (DataRow dr3 in dt.Rows)
            {

                rows = new Dictionary<string, object>();

                int i = 1;
                foreach (DataColumn col in dt.Columns)
                {
                    rows.Add(col.ColumnName, dr3[col].ToString());
                    i = i + 1;

                }
                tableRow.Add(rows);

            }
            resultJSON = serializer.Serialize(tableRow).ToString();

        }
        catch (Exception ex)
        {

            resultJSON = ex.Message.ToString();

        }

        Context.Response.Write(resultJSON);

    }















Ajax电话代码: -












Ajax call code:-


 <script type="text/javascript">
      
       $(function () {
           $.ajax({
               url: 'WebService.asmx/GetDatBooth',
               type: 'POST',
               dataType: 'json', //make sure your service is actually returning json here
               contentType: 'application/json',
               success: function (data, status) {
                   alert(data);
                   //here data is whatever your WebService.asmx/getList returned
                   //populate your dropdown here with your $.each w/e
               }
           });
       });
</script>  





我的尝试:



如何通过ajax / json调用webservice json数据



What I have tried:

how to call webservice json data through ajax/json

推荐答案

(function(){
(function () {


.ajax({
url:'WebService.asmx / GetDatBooth',
类型:'POST',
dataType:'json',//确保你的服务实际上是在这里返回json
contentType:' application / json',
success:function(data,status){
alert(data);
//这里数据是你的WebService.asmx / getList返回的
//使用
.ajax({ url: 'WebService.asmx/GetDatBooth', type: 'POST', dataType: 'json', //make sure your service is actually returning json here contentType: 'application/json', success: function (data, status) { alert(data); //here data is whatever your WebService.asmx/getList returned //populate your dropdown here with your


.each w / e
}
});
});填写您的下拉列表。
< / script>
.each w/e } }); }); </script>





我的尝试:



如何通过ajax / json调用webservice json数据



What I have tried:

how to call webservice json data through ajax/json


这篇关于如何通过ajax / json调用webservice的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆