Problem retrieving json data with jQuery.ajax

Discussion in 'HTML / PHP / JavaScript / CSS' started by babuman, Jun 17, 2009.

  1. Hi everyone,
    I am very new to jQuery (novice to JavaScript for that matter). I am trying to retrieve some json data from an ASP.NET Web Service Method. I have been banging my head for the past few days with no sucess. I have placed breakpoints both inside the jquery.ajax call and also in my WebService. But it seems like my Web Method never gets called. I also have javascript alerts in both the Success and Failure events of my ajax call. None of them display. And in my GetTaxonomies() function I have declared two variables to hold the reponse in two different place (just for test purposes). Both of them are "undefined" after the script runs.
    Can somebody please tell me what am I doing wrong here? I have been strugging with this for many days now.
    Here is my aspx page!
    <%@ Page Language="C#" AutoEventWireup="true" CodeFile="TreeView.aspx.cs" Inherits="TreeView" %>
    <!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
    <html xmlns="http://www.w3.org/1999/xhtml">
    <head runat="server">
    <title></title>
    <script src="Scripts/jquery-1.3.2.js" type="text/javascript"></script>
    </head>
    <body>
    <script type="text/javascript">
    $(document).ready(function()
    {
    var taxJson = GetTaxonomies();
    });
    function GetTaxonomies()
    {
    alert("In GetTaxonomies");
    var temp = { };
    $.ajax({
    type: "POST",
    url: "TreeViewService.asmx/Taxonomies",
    data: "{}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function(response)
    {
    alert("Success");
    temp = response.d;
    return response;
    },
    failure: function(msg)
    {
    alert(msg.d);
    }
    });
    }; </script>
    <form id="form1" runat="server">
    </form>
    </body>
    </html>
    And here is my Web Service class.
    using System;
    using System.Collections;
    using System.Collections.Generic;
    using System.Linq;
    using System.Web;
    using System.Web.Services;
    using System.Web.Script.Services;
    using DynaPro;
    /// <summary>
    /// Summary description for TreeViewService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    // 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 TreeViewService : System.Web.Services.WebService
    {
    public TreeViewService()
    {
    //Uncomment the following line if using designed components
    //InitializeComponent();
    }
    [WebMethod]
    [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
    public List<TaxEntity> Taxonomies()
    {
    //ArrayList tax = new ArrayList();
    List<TaxEntity> tax = new List<TaxEntity>();
    TaxonomiesDataContext taxDC = new TaxonomiesDataContext();
    IEnumerable<Taxonomy> taxonomies = taxDC.Taxonomies.AsEnumerable();
    foreach( Taxonomy oTax in taxonomies)
    {
    TaxEntity ent = new TaxEntity();
    ent.Key = (int)oTax.PKId;
    ent.ParentKey = (int)oTax.ParentId;
    ent.Name = oTax.Name;
    tax.Add(ent);
    }
    return tax;
    }
    }
    And here is my TaxEntity class.
    public class TaxEntity
    {
    public int Key;
    public int ParentKey;
    public string Name;
    public string TableName;
    public TaxEntity(){}
    }
     
  2. Did you find a solution yet? I must admit I have little experience with JQuery, so little that it's best to say I have no useful JQuery knowledge whatsoever and I'll attempt to offer alternative advice.

    Since JSON is often coupled with AJAX and the OP does describe that type of application, I suggest that a WCF JSON enabled service coupled with ASP.NET AJAX is the way to go. This approach would certainly suit me because it means I don't have to go and learn how to achieve this functionality / go anywhere near JQuery - in addition there is also already a large amount of information available online on how to do this in WCF / ASP.NET AJAX e.g. MSDN, Channel9 and other locations.

    This is an excellent webcast on AJAX enabling WCF services: http://www.pluralsight.com/main/screencasts/screencast.aspx?id=enabling-ajax-integration and this is just one of a whole series of excellent free WCF webcasts from Aaron Skonnard at Pluralsight.

    HTH
     

Share This Page