SitePoint Sponsor

User Tag List

Results 1 to 4 of 4
  1. #1
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)

    Question Jquery Ajax and WebService

    Hi

    I have Web Services that get all data from SQL server and I create HTML table to dispaly all data using Jqurry Ajax but I have problem with ajax. can any one help me

    JQuery

    $(document).ready(function () {
    $("#contant_hr").append('<table id="ResultsTable" class="DepartmentTable"><tr><th>Department ID</th><th>Name</th><th>Group Name</th><th>Modified Date</th></tr>');
    $.ajax({
    type: "POST",
    contentType: "application/json; charset=utf-8",
    url: "Department_Service.asmx/GetAllDepartments",
    dataType: "json",
    success: function (msg) {
    alert('DFGFD');
    var c = eval(msg.d);
    for (var i in c) {
    $("#ResultsTable tr:last").after("<tr><td>" + c[i][0] + "</td><td>" + c[i][1] + "</td><td>" + "</td><td>" + c[i][2] + "</td><td>" + c[i][3] + "</td></tr>");
    }
    }

    });
    });


    WebService

    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public string GetAllDepartments() {
    SqlConnection conn = new SqlConnection("Data Source=ABDALLA-HP\\SQLEXPRESS;Initial Catalog=AdventureWorks;Integrated Security=True;");

    string sqlstring = "SELECT DepartmentID, Name, GroupName, ModifiedDate FROM HumanResources.Department";
    SqlCommand comm = new SqlCommand(sqlstring,conn);

    DataSet dataset = new DataSet();
    SqlDataAdapter adapter = new SqlDataAdapter(comm);
    adapter.Fill(dataset, "reading");

    conn.Close();

    //creating Array
    string[][] jaggedArray = new string[dataset.Tables[0].Rows.Count][];
    int i = 0;
    foreach (DataRow rs in dataset.Tables[0].Rows)
    {
    jaggedArray[i] = new string[] { rs["DepartmentID"].ToString(), rs["Name"].ToString(), rs["GroupName"].ToString(), rs["ModifiedDate"].ToString() };
    i = i + 1;


    }

    //return JSOM data
    JavaScriptSerializer js = new JavaScriptSerializer();
    string strJSON = js.Serialize(jaggedArray);
    return strJSON;


    }

  2. #2
    SitePoint Mentor NightStalker-DNS's Avatar
    Join Date
    Jul 2004
    Location
    Cape Town, South Africa
    Posts
    2,868
    Mentioned
    37 Post(s)
    Tagged
    0 Thread(s)
    Well, what exactly is it doing? I have never used a web method for returning jSON to jQuery before. Usually just a call to an aspx page with the correct parameters if any, which formats the response as json. If you browse directly from your browser to Department_Service.asmx/GetAllDepartments, does it load the data? I dout it, as you need to interact differently with a webservice.

    Change it to use an aspx page and see if that works

  3. #3
    SitePoint Member
    Join Date
    May 2011
    Posts
    5
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    you can make an AJAX request like any other requests:

    $.ajax( {
    type:'Get',
    url:'http://mysite.com/mywebservice',
    success:function(data) {
    alert(data);
    }

    })

  4. #4
    SitePoint Enthusiast
    Join Date
    Oct 2010
    Posts
    60
    Mentioned
    0 Post(s)
    Tagged
    0 Thread(s)
    thanks


Bookmarks

Posting Permissions

  • You may not post new threads
  • You may not post replies
  • You may not post attachments
  • You may not edit your posts
  •