gene February 2016

Ajax success call is not executing

I have the following AJAX call:

   $.ajax({
        type: "POST",
        url: "Default.aspx/GeneratePdfs",
        data: '{frequency:   "' + $('#ddlReportFrequency option:selected').text() + 
               ', reportYear: "' + $('#txtReportYear').text() + 
               ', reportMonth: "' + $('#txtReportMonth').text() + 
               ', reportDay:   "' + $('#txtReportDay').text() +
               ', reportOption: "' + $('#ddlReportOption option:selected').text() +
               ', reportRegion: "' + $('#txtReportRegion').text() +
               ', reportSchedule: "' + $('#ddlReportSchedule').text() + 
               ', reportRegion:   "' + $('#txtReportRegion').text() + '"}', 
        contentType: "application/json; charset=utf-8",
        //            dataType: "json",
        success: function (data) {
            debugger;
            if (data.d != "") {
                $('#rptDisplay').text(data.d);
            }

            alert("1");
        },
        failure: function () {
            //                $('#rptDisplay').text("Error");
            alert("2");
        }

I think I made some mistake sending the parameters since the success section is not called.

What am I doing wrong here?

Answers


BenG February 2016

try changing data to an actual object:-

data: {frequency: $('#ddlReportFrequency option:selected').text(), 
       reportYear: $('#txtReportYear').text(), 
       reportMonth: $('#txtReportMonth').text(), 
       reportDay: $('#txtReportDay').text(),
       reportOption: $('#ddlReportOption option:selected').text(),
       reportRegion: $('#txtReportRegion').text(),
       reportSchedule: $('#ddlReportSchedule').text(),
       reportRegion: $('#txtReportRegion').text()},


phenxd February 2016

You could try using

data: JSON.stringify({
    frequency: $('#ddlReportFrequency option:selected').text(),
    ...
}),

Not sure if you will have to remove the charset part in here.

contentType: "application/json; charset=utf-8"


Brett84c February 2016

I would build the JSON object ahead of time like this. I removed unnecessary code and simplified it a little. If the there is a problem in the call, the 'error' function is called, not 'failure'. Make sure the URL you are calling is valid. I'm assuming this script is being run in the same folder where the aspx file exists.

var jsonObject = { 
  frequency: $('#ddlReportFrequency option:selected').text(),
  reportYear: $('#txtReportYear').text(),
  reportMonth: $('#txtReportMonth').text(),
  reportDay: $('#txtReportDay').text(),
  reportOption: $('#ddlReportOption option:selected').text(),
  reportRegion: $('#txtReportRegion').text(),
  reportSchedule: $('#ddlReportSchedule').text(),
  reportRegion: $('#txtReportRegion').text()
};

$.ajax({
    type: "POST",
    url: "Default.aspx/GeneratePdfs",
    data: jsonObject, 
    dataType: "json",
    success: function (data) {
        alert("success");
        console.log(data);
    },
    error: function (err) {
        alert("error");
        console.log(err);
    }
});

Of course, you could also just build an Object Literal directly inside the 'data' node of the AJAX call like this:

 $.ajax({
    type: "POST",
    url: "Default.aspx/GeneratePdfs",
    data: { 
      frequency: $('#ddlReportFrequency option:selected').text(),
      reportYear: $('#txtReportYear').text(),
      reportMonth: $('#txtReportMonth').text(),
      reportDay: $('#txtReportDay').text(),
      reportOption: $('#ddlReportOption option:selected').text(),
      reportRegion: $('#txtReportRegion').text(),
      reportSchedule: $('#ddlReportSchedule').text(),
      reportRegion: $('#txtReportRegion').text()
    }, 
    dataType: "json",
    success: function (data) {
        alert("success");
        console.log(data);
    },
    error: function (err) {
        alert("error");
        console.log(err);
    }
});

Either way works, the second way might be slightly more efficient sinc


user2344183 February 2016

Your double quotes in the data object don't match up. Add an end double quote before the start of your next property.

data: '{frequency:   "' + $('#ddlReportFrequency option:selected').text() + 
           '", reportYear: "' + $('#txtReportYear').text() + 
           '", reportMonth: "' + $('#txtReportMonth').text() + 
           '", reportDay:   "' + $('#txtReportDay').text() +
           '", reportOption: "' + $('#ddlReportOption option:selected').text() +
           '", reportRegion: "' + $('#txtReportRegion').text() +
           '", reportSchedule: "' + $('#ddlReportSchedule').text() + 
           '", reportRegion:   "' + $('#txtReportRegion').text() + '"}', 


flacle February 2016

There seem to be syntax issues with the way you constructed your JSON data object. There are double quotes missing to enclose the values and there aren't any double quotes around the strings, try rewriting it as such:

$.ajax({
  type: "POST",
  url: "Default.aspx/GeneratePdfs",
  data: '{"frequency": "' + $('#ddlReportFrequency option:selected').text() + 
        '", "reportYear": "' + $('#txtReportYear').text() + 
        '", "reportMonth": "' + $('#txtReportMonth').text() + 
        '", "reportDay": "' + $('#txtReportDay').text() +
        '", "reportOption": "' + $('#ddlReportOption option:selected').text() +
        '", "reportRegion": "' + $('#txtReportRegion').text() +
        '", "reportSchedule": "' + $('#ddlReportSchedule').text() + 
        '", "reportRegion": "' + $('#txtReportRegion').text() + '"}', 
  contentType: "application/json; charset=utf-8",
  // dataType: "json",
  success: function (data) {
    debugger;
    if (data.d != "") {
      $('#rptDisplay').text(data.d);
    }
    alert("1");
  },
  failure: function () {
    // $('#rptDisplay').text("Error");
    alert("2");
  }
});

See: http://www.json.org ;)

Post Status

Asked in February 2016
Viewed 3,041 times
Voted 4
Answered 5 times

Search




Leave an answer