babuman
06-17-2009, 11:44 AM
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(){}
}
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(){}
}