Failed to get response from webservice method using json and ajax call

I created a webservice and it has two methods and when I call the first method it works, but when I call the getpostings method I cannot get the response but I can call the method

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;
using System.Data;
using System.Data.SqlClient;
using System.Web.Script.Services;
using System.Web.Script.Serialization;


   namespace SimpleService
{
public class Errors
{
    public int id { get; set; }
    public int data { get; set; }
}
/// <summary>
/// Summary description for Service1
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
// To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
 [System.Web.Script.Services.ScriptService]
public class Service1 : System.Web.Services.WebService
{
    [WebMethod]
    public   string GetCurrentTime(string name)
    {
        return "Hello " + name + Environment.NewLine + "The Current Time is: "
            + DateTime.Now.ToString();
    }

    [WebMethod]
    //[ScriptMethod(UseHttpGet=true)]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json,UseHttpGet = true)]
    public   List<Errors> GetPostings()
    {

        string connetionString = null;
        SqlConnection cnn;
        connetionString = "Data Source=mycomputer.\\SCOM;Initial Catalog=Portal;Integrated Security=True";
        cnn = new SqlConnection(connetionString);

        const string SQL = "select id,openemr from openemrdata";
        SqlCommand myCommand = new SqlCommand(SQL, cnn);
        cnn.Open();
        myCommand.ExecuteNonQuery();
        // myConnection.Close();

        SqlDataAdapter dataAdapter = new SqlDataAdapter();
        dataAdapter.SelectCommand = myCommand;

        DataSet DSet = new DataSet();
        dataAdapter.Fill(DSet);
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(JaggedArray);

        List<Errors> errors = new List<Errors>();
        foreach (DataRow row in DSet.Tables[0].Rows)
        {
            Errors jp = new Errors();

            jp.id = (int)row["id"];
            jp.data = (int)row["openemr"];
            //jp.Id = (int)row["Id"];
            //jp.JobPost = row["JobPosting"].ToString();
            //jp.Applicant = row["Applicant"].ToString();
            //jp.Type = row["Type"].ToString();
            errors.Add(jp);
        }


        return errors;


    }
}

      

}

here is java script code

`<script type = "text/javascript">
    function ShowCurrentTime() {
        alert(1);
        $.ajax({
            type: "GET",
            url: "~/Service1.asmx/GetPostings",
            data: '',
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: OnSuccess,
        failure: function (response) {
            alert(1);
            alert(response.d);
        }
    });
}
function OnSuccess(response) {

    alert(1);
        var myData = response.d;
        var data1;
        var data2;
        alert(suresh);
        alert(myData.length + " hellos");
        for (var i = 0; i < myData.length; i++) {

            $("#chart").append(myData[i].id + " " + myData[i].data);
            var list = myData;
        }
        for(var i=0;i<list.length;i++)
        {
            $("#chart").append( list[i].openemr);
        };
}
</script>`

      

+3


source to share


2 answers


To make a web service consumed by JSON there are two things that need to be done for the web service.

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetPage(string pagenam)
    {
        string[] JaggedArray = arr; 
        JavaScriptSerializer js = new JavaScriptSerializer();
        string strJSON = js.Serialize(JaggedArray);
        return strJSON;
    }

      

This is a distilled example from one of my own web services. You can see that there is an annotation for the ResponseFormat for the web service and array serialization. This should work with yours.



Remmeber to add the necessary links and links.

using System.Web.Script.Services;
using System.Web.Script.Serialization;

      

+1


source


Use POST instead of GET to decorate your web method with the following attributes

[WebMethod]
[ScriptMethod(UseHttpGet = true)]

      



also add the following to web.config

<webServices>
  <protocols>
    <add name="HttpGet"/>
    <add name="HttpPost"/>
  </protocols>
</webServices>

      

0


source







All Articles