Nishant.Chauhan80 Ответов: 0

Как вызвать webservice через ajax/json


код веб-сервиса:



[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:-


 <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>  


Что я уже пробовал:

как вызвать webservice json data через ajax/json

MadMyche

И что происходит, когда эта функция AJAX выполняется?

0 Ответов