Simon Price
Simon Price

Reputation: 3261

ASP.Net WebForms Ajax A circular reference was detected while serializing an object of type 'System.Reflection.Module'

I am using an ASP.Net Webforms project and am using JQuery to do a post of data and get a result of true or false back from the database following the entry of data.

I must stress that this works locally and on the test server but not in production.

Each of the areas of the web config file that I needed to change in Test and locally are the same in production too.

This is the JQuery

$("#bttnSave").click(function () {

    $("#successAlert").hide();
    $("#errorAlert").hide();

    $(this).hide();
    var div = "#action";
    $(div).hide();
    $("#saving").show();

    var singleV = 0.00;
    var annualVal = 0.00;
    var quarterlyVal = 0.00;
    var cover = $('#ddlCoverTypes :selected').val();


    if ($("#txtSinglePaymentValue").val() !== "") {
        singleV = parseFloat($("#txtSinglePaymentValue").val());
    }
    if ($("#txtAnnualPaymentValue").val() !== "") {
        annualVal = parseFloat($("#txtAnnualPaymentValue").val());
    }
    if ($("#txtQuarterlyPaymentValue").val() !== "") {
        quarterlyVal = parseFloat($("#txtQuarterlyPaymentValue").val());
    }

    var jsonObject = {
        coverType: cover,
        singleVaL: singleV.toFixed(2),
        annual: annualVal.toFixed(2),
        quarterly:quarterlyVal.toFixed(2),
        csvStrMarkets:$("#ddlMarkets").val()
    };


    var url = "EQS690.aspx/BulkSaveMarketRates";
    var errorCode = "BLK-MKT-UPDT";
    console.log(jsonObject);
    setTimeout(
        function () {
            ajaxCall(url, jsonObject, errorCode, div, "#bttnSave");
        }, 1000);


});



function ajaxCall(appUrl, jsonObject, errorCode, div, button) {


    $.ajax({
        type: "POST",
        url: appUrl,
        data: JSON.stringify(jsonObject),
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (result) {
            $("#saving").hide();
            $(div).show();
            $(button).show();
            $("#successAlert").show();
            $("#errorAlert").hide();
            return true;
        },
        error: function (result) {
            console.log(result);

            var json = $.parseJSON(result.responseText);

            $("#lblError").html(
                "Something went wrong with saving your changes. If the problem continues please report the issue to the system administrator. <br/><br/>When reporting the issue please give error code <br/><b>" +
                errorCode + ": " + json.Message + "</b>");
            $("#saving").hide();
            $(div).show();
            $(button).show();
            $("#successAlert").hide();
            $("#errorAlert").show();

            return result;
        }
    });
}

This is the .Net code

[ScriptMethod]
    [System.Web.Services.WebMethod]
    public static object BulkSaveMarketRates(string coverType, decimal singleVaL, decimal annual, decimal quarterly, List<string> csvStrMarkets)
    {
        var listOfParams = new List<SqlParameter>();

        var coverParam = new SqlParameter("@CoverType", SqlDbType.Char);
        var singleParam = new SqlParameter("@Single", SqlDbType.Decimal);
        var annualParam = new SqlParameter("@Annual", SqlDbType.Decimal);
        var quarterlyParam = new SqlParameter("@Quarterly", SqlDbType.Decimal);
        var marketsParam = new SqlParameter("@Markets", SqlDbType.VarChar);

        coverParam.Value = coverType;
        singleParam.Value = singleVaL;
        annualParam.Value = annual;
        quarterlyParam.Value = quarterly;

        var sb = new StringBuilder();

        foreach (var market in csvStrMarkets)
        {
            sb.Append($"{market}, ");
        }

        marketsParam.Value = sb.ToString().Trim().TrimEnd(',');

        listOfParams.Add(coverParam);
        listOfParams.Add(singleParam);
        listOfParams.Add(annualParam);
        listOfParams.Add(quarterlyParam);
        listOfParams.Add(marketsParam);

        return InsertOrUpdateRecord(listOfParams, "sp_UpdateMarketParametersAdminFee", false);
    }

This is the error that I am getting

A circular reference was detected while serializing an object of type 'System.Reflection.Module'.

Upvotes: 0

Views: 535

Answers (1)

Cooleshwar
Cooleshwar

Reputation: 417

I think the return type object has something which is not going well with serialization. Your return object from the webmethod is not serialized at this time. You may want to try this -

return new JavaScriptSerializer().Serialize(new { errMsg = "test" });

Inclusion of System.Web.Script.Serialization is necessary for this. With this, your result in the return object is serialized and it will be received well with this line in your javascript -

var json = $.parseJSON(result.responseText);

Upvotes: 1

Related Questions