3

I have created a webservice and it has two methods and when i call the first method it is working but when i call the getpostings method am unable to get the response but able to 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>`
7
  • Does it work with SoapUI ? or other similar. Commented Oct 10, 2014 at 8:19
  • What are you using? ASP.NET or ASP.MVC? Commented Oct 10, 2014 at 8:19
  • NO it doesn't work and isthere anything to be added in the web.config file? Commented Oct 10, 2014 at 8:20
  • asp.net @ Valentyn Vynogradskiy Commented Oct 10, 2014 at 8:20
  • Try and add [ScriptMethod(ResponseFormat = ResponseFormat.Json)] to below the webservice annotation Commented Oct 10, 2014 at 8:21

2 Answers 2

1

To make a webservice consumable by JSON there is 2 things that needs to be done to the webservice.

    [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 webservices. You see that there is a ResponseFormat Annotation to the webservice and a serialization to the array. This should work with yours to.

Remmeber to add the reqired usings and references.

using System.Web.Script.Services;
using System.Web.Script.Serialization;
Sign up to request clarification or add additional context in comments.

Comments

0

Use POST instead of GET else decorate your webmethode with following attributes

[WebMethod]
[ScriptMethod(UseHttpGet = true)]

also add following in web.config

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

6 Comments

call is being made but the response is not being made,unable to receive the response
add another attribute [ScriptMethod(ResponseFormat = ResponseFormat.Json)] use this. if the requestd response format is json
what is working function ? fail function or success
"unable to receive the response" what you mean by this, whether your success function in ajax call working but no response or failure function is working..
it is not even going to any of the functions,it is just calling the web method and the page displays nothing
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.