Click here to Skip to main content
15,889,992 members
Please Sign up or sign in to vote.
1.22/5 (2 votes)
See more:
I'm hitting the "error" part of this ajax call, seeing "'error in btnTestProduceUsageSettings'":

JavaScript
$("#btnTestProduceUsageSettings").click(function () {
            var dom = $('#dayofmonthselect').val();
            var ord = $('#ordinalselect').val();
            var dow = $('#dayofweekselect').val();
            var wom = $('#weekormonthselect').val();
            var daterangefromval = $('#produsagefrom').val(); 
            var daterangetoval = $('#produsageto').val();
            var unitval = $('#unitsselect').val();
            var reportId = 1;

            $.ajax({
                type: 'GET',
                url: '@Url.Action("GetTestSettingVals", "TestSettings")',
                data: { unit: unitval, report: reportId, dayofmonth: dom, ordinal: ord, dayofweek: dow, 
weekormonth: wom, fromval: daterangefromval, toval: daterangetoval },
                cache: false,
                success: function (returneddata) {
                    var nextgendate = returneddata.testsettings.NextGenDate;
                    var nextfromdate = returneddata.testsettings.NextFromDate;
                    var nexttodate = returneddata.testsettings.NextToDate;

                    alert('If you save the current configuration, the Produce Usage report would next be sent on ' + nextgendate + ' and cover data from ' + nextfromdate + ' to ' + nexttodate);
                },
                error: function () {
                    alert('error in btnTestProduceUsageSettings');
                }
            });

        }); 


JSLint doesn't find anything really wrong with it (beyond its usual nitpickiness). At least one of the params is always null. I wondered it that was the problem, but I tried this, just to make sure the null values were not causing the problem:

. . .
var ord = ''; //$('#ordinalselect').val();
var dow = ''; //$('#dayofweekselect').val();
var wom = ''; //$('#weekormonthselect').val();
. . .

...and it still failed.

The Controller method that is supposed to be called isn't being reached at all because of the failure. It is defined:

public JsonResult GetTestSettingVals(string unit, int RptID, string dayOfMonthGen,
string ordinalGen, string dayOfWeekGen, string weekOrMonthGen,
int fromIntDateRange, int toIntDateRange)

So stepping through the code, with a breakpoint in Chrom Dev Tools, I reach the "$.ajax({" line, but not the breakpoint in the Controller method (in Visual Studio), which has one on the last line shown below:

public class TestSettingsController : Controller
{
public JsonResult GetTestSettingVals(string unit, int RptID, string dayOfMonthGen, string ordinalGen, string dayOfWeekGen, string weekOrMonthGen, int fromIntDateRange, int toIntDateRange)
{
TestSettingsModel model = new TestSettingsModel();

I have other ajax/controller combinations designed the same way that work fine, so I can't see what the hangup is here...am I missing an either obvious or well-concealed problem here?

What I have tried:

I wondered if sending some null vals might be the problem, but I tried this, just to make sure the null values were not causing the problem:

. . .
var ord = ''; //$('#ordinalselect').val();
var dow = ''; //$('#dayofweekselect').val();
var wom = ''; //$('#weekormonthselect').val();
. . .

...and it still failed.

The Controller method that is supposed to be called isn't being reached at all because of the failure. It is defined:

public JsonResult GetTestSettingVals(string unit, int RptID, string dayOfMonthGen,
string ordinalGen, string dayOfWeekGen, string weekOrMonthGen,
int fromIntDateRange, int toIntDateRange)

So stepping through the code, with a breakpoint in Chrom Dev Tools, I reach the "$.ajax({" line, but not the breakpoint in the Controller method (in Visual Studio), which has one on the last line shown below:

public class TestSettingsController : Controller
{
public JsonResult GetTestSettingVals(string unit, int RptID, string dayOfMonthGen, string ordinalGen, string dayOfWeekGen, string weekOrMonthGen, int fromIntDateRange, int toIntDateRange)
{
TestSettingsModel model = new TestSettingsModel();

I have other ajax/controller combinations designed the same way that work fine, so I can't see what the hangup is here...am I missing an either obvious or well-concealed problem here?
Posted
Updated 2-May-16 22:00pm
Comments
Afzaal Ahmad Zeeshan 28-Apr-16 19:57pm    
I suggest that you check the console, error messages are typically logged there. The error in the JavaScript maybe because of some problem in the syntax or something... F12?
ZurdoDev 28-Apr-16 20:08pm    
Your error function can take 3 arguments and the 2nd will have the details. Add arguments to your error function and then examine the 2nd one.

1 solution

Hi B. Clay Shannon,

Few points to remember while making ajax call to controller:
1. The argument name in ajax call i.e. in the data section should match exactly with that of in the controller.
2. It should also match the exact casing as well.

I have analysed your code and wrote a test application on the same.
Your code was not working because the arguments were not matching.

Below is the detailed working & tested code.

Note: I have prefixed controller arguments with c_ to distinguish.

In Controller:

C#
[HttpGet]
       public JsonResult GetTestSettingVals(string c_Unit, int c_RptID, string c_DayOfMonthGen, string c_OrdinalGen, string c_DayOfWeekGen, string c_WeekOrMonthGen, int c_FromIntDateRange, int c_ToIntDateRange)
       {
           try
           {
               string myCustomJson = " c_Unit: " + c_Unit + ",c_RptID: " + c_RptID.ToString() + ",c_DayOfMonthGen: " + c_DayOfMonthGen + ",c_OrdinalGen: " + c_OrdinalGen + ",c_DayOfWeekGen: " + c_DayOfWeekGen + ",c_WeekOrMonthGen: " + c_WeekOrMonthGen + ",c_FromIntDateRange: " + c_FromIntDateRange.ToString() + ",c_ToIntDateRange: " + c_ToIntDateRange.ToString();
               return Json(new { DataFromController = myCustomJson}, JsonRequestBehavior.AllowGet);
           }
           catch (Exception)
           {

           }
           return null;
       }


In Javascript:

JavaScript
<script type="text/javascript">
    $(document).ready( function() {
        debugger
            var unitval = 'myUnit';
            var reportId = 1;
            var dom = 'myDayOfMonth';
            var ord = 'myordinalGen';
            var dow = 'myDayOfWeek';
            var wom = 'myWeekOrMonth';
            var daterangefromval = 2;
            var daterangetoval = 3;

            $.ajax({
                type: 'GET',
                url: '@Url.Action("GetTestSettingVals", "Home")',
                data: {
                    c_Unit: unitval, c_RptID: reportId, c_DayOfMonthGen: dom, c_OrdinalGen: ord, c_DayOfWeekGen: dow,
                    c_WeekOrMonthGen: wom, c_FromIntDateRange: daterangefromval, c_ToIntDateRange: daterangetoval
                },
                cache: false,
                success: function (data) {debugger
                    var myDataFromController = data.DataFromController;
                    alert(myDataFromController);
                },
                error: function (response) {
                            alert('in error');
                            alert(response.responseText);
                        },
                failure: function (response) {
                            alert('in failure')
                            alert(response.responseText);
                        }
            });
               

    });
   
</script>


Hope it helps you and others who encounter the same issue.

Do reply & mark as answer if it works.


Thanks,
Prateek
 
Share this answer
 
v2

This content, along with any associated source code and files, is licensed under The Code Project Open License (CPOL)



CodeProject, 20 Bay Street, 11th Floor Toronto, Ontario, Canada M5J 2N8 +1 (416) 849-8900