I'm trying to do pretty much exactly what this example (in the link shown below) shows. And I get the error of "A circular reference was detected...".
http://ajax.asp.net/docs/tutorials/ConsumingWebServicesWithAJAXTutorial.aspx - Passing and Returning Complex Types - 2nd example
Does anyone know why this error occurs? Looking at the example (from the link), I would think that would generate the error, but it doesn't.
Essentially what I'm trying to do is:
In Javascript:
-
Collect form values from the HTML via Javascript
-
build an object of a specific type
-
set the properties
-
send the javascript object to a webservice method as an argument
-
then return the result from the webservice as a JSON object
Here's my ASP.NET code:
<%@ Page Language="C#" MasterPageFile="~/MasterPage.master" AutoEventWireup="true" CodeFile="jsontest.aspx.cs" Inherits="Products_jsontest" Title="JSON Test" %>
<%@ Import Namespace="System.Web.Script.Serialization" %>
<asp:Content ID="Content1" ContentPlaceHolderID="ContentPlaceHolder1" Runat="Server">
<asp:ScriptManager ID="scriptManager" runat="server">
<Scripts>
<asp:ScriptReference Path="~/scripts/handleproducts.js" />
</Scripts>
<Services>
<asp:ServiceReference Path="~/ws/idcwebservice.asmx" />
</Services>
</asp:ScriptManager>
<br />
<asp:DropDownList ID="ddlColors" runat="server" onchange="getProductData(this);">
</asp:DropDownList><br />
<br />
<div id="hdnProductID"></div>
<div id="boxOptionsInnerBoxPricingTotalPrice"></div>
<div id="boxOptionsInnerBoxPricingUnitPrice"></div>
<div id="errorMsg"></div>
</asp:Content>
Here's my Javascript code:
function getProductData(ctrl){
var selectedIndex = ctrl.selectedIndex;
var selectedColor = ctrl.options[selectedIndex].value;
try{
var prod = new IDCPrinting.ProductData();
prod.ColorTypeID = selectedColor;
prod.SizeTypeID = 1;
prod.Quantity = 500;
prod.CoatingTypeID = 1;
prod.StockTypeID = 1;
prod.Proof = false;
prod.TurnaroundTypeID = 1;
//alert(prod.Quantity);
IDCPrinting.IDCPrintingWebService.GetProduct(prod, OnProductSucceededCallback, OnProductFailedCallback);
}catch(e){
alert("Error [getProductData]: " + e.message);
}
}
// Callback function invoked on success
function OnProductSucceededCallback(result, userContext, methodName){
switch(methodName){
case "GetProduct" :
{
var newObj = Sys.Serialization.JavaScriptSerializer.deserialize(result);
var divHdnProductID = document.getElementById("hdnProductID");
var divTotalPrice = document.getElementById("boxOptionsInnerBoxPricingTotalPrice");
var divUnitPrice = document.getElementById("boxOptionsInnerBoxPricingUnitPrice");
divHdnProductID.value = newObj.productID
divUnitPrice.innerHTML = calcUnitPrice(newObj.price, quan);
divTotalPrice.innerHTML = getProductTotal(newObj.price);
break;
}
}
}
// Callback function invoked on failure
// of the Web service methods.
function OnProductFailedCallback(error, userContext, methodName)
{
if(error !== null)
{
var divError = document.getElementById("errorMsg");
divError.innerHTML = "An error occurred: " + error.get_message();
}
}
if(typeof('Sys') !== 'undefined') Sys.Application.notifyScriptLoaded();
Here's my webservice code:
using System;
using System.Web;
using System.Collections;
using System.Web.Services;
using System.Web.Services.Protocols;
using System.Web.Script.Services;
using System.Web.Script.Serialization;
using System.Collections.Generic;
using IDC.BusinessObjects;
namespace IDC
{
#region ProductData class
public class ProductData
{
public ProductData()
{
}
private int _ColorTypeID;
public int ColorTypeID
{
get { return _ColorTypeID; }
set { _ColorTypeID = value; }
}
private int _SizeTypeID;
public int SizeTypeID
{
get { return _SizeTypeID; }
set { _SizeTypeID = value; }
}
private int _Quantity;
public int Quantity
{
get { return _Quantity; }
set { _Quantity = value; }
}
private int _CoatingTypeID;
public int CoatingTypeID
{
get { return _CoatingTypeID; }
set { _CoatingTypeID = value; }
}
private int _StockTypeID;
public int StockTypeID
{
get { return _StockTypeID; }
set { _StockTypeID = value; }
}
private bool _Proof;
public bool Proof
{
get { return _Proof; }
set { _Proof = value; }
}
private int _TurnaroundTypeID;
public int TurnaroundTypeID
{
get { return _TurnaroundTypeID; }
set { _TurnaroundTypeID = value; }
}
private int _AddressingTypeID;
public int AddressingTypeID
{
get { return _AddressingTypeID; }
set { _AddressingTypeID = value; }
}
private int _BindingFinishingTypeID;
public int BindingFinishingTypeID
{
get { return _BindingFinishingTypeID; }
set { _BindingFinishingTypeID = value; }
}
private int _CardSlotTypeID;
public int CardSlotTypeID
{
get { return _CardSlotTypeID; }
set { _CardSlotTypeID = value; }
}
private int _CoverPagesTypeID;
public int CoverPagesTypeID
{
get { return _CoverPagesTypeID; }
set { _CoverPagesTypeID = value; }
}
private int _FoldingTypeID;
public int FoldingTypeID
{
get { return _FoldingTypeID; }
set { _FoldingTypeID = value; }
}
private int _InsidePagesTypeID;
public int InsidePagesTypeID
{
get { return _InsidePagesTypeID; }
set { _InsidePagesTypeID = value; }
}
private int _NumberOfMonthsTypeID;
public int NumberOfMonthsTypeID
{
get { return _NumberOfMonthsTypeID; }
set { _NumberOfMonthsTypeID = value; }
}
private int _PagingTypeID;
public int PagingTypeID
{
get { return _PagingTypeID; }
set { _PagingTypeID = value; }
}
private int _SecondSheetTypeID;
public int SecondSheetTypeID
{
get { return _SecondSheetTypeID; }
set { _SecondSheetTypeID = value; }
}
private bool _Perforation;
public bool Perforation
{
get { return _Perforation; }
set { _Perforation = value; }
}
private bool _Scoring;
public bool Scoring
{
get { return _Scoring; }
set { _Scoring = value; }
}
private bool _Tabbing;
public bool Tabbing
{
get { return _Tabbing; }
set { _Tabbing = value; }
}
}
#endregion
/// <summary>
/// Summary description for IDCWebService
/// </summary>
[WebService(Namespace = "http://idc.com/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[GenerateScriptType(typeof(ProductData))]
[ScriptService]
public class IDCWebService : System.Web.Services.WebService
{
public IDCWebService()
{
//Uncomment the following line if using designed components
//InitializeComponent();
}
[WebMethod]
public object GetProduct(ProductData prod)
{
Products p = new Products();
p.Query.Where(
p.Query.ColorTypeID.Equal(prod.ColorTypeID),
p.Query.SizeTypeID.Equal(prod.SizeTypeID),
p.Query.Quantity.Equal(prod.Quantity),
p.Query.CoatingTypeID.Equal(prod.CoatingTypeID),
p.Query.StockTypeID.Equal(prod.StockTypeID),
p.Query.Proof.Equal(prod.Proof),
p.Query.TurnaroundTypeID.Equal(prod.TurnaroundTypeID));
p.Query.Load();
JavaScriptSerializer serializer = new JavaScriptSerializer();
return serializer.Serialize(p);
//return p;
}
}
}
Any help would be appreciated.
Thanks.