Ajax成功调用未执行

时间:2016-02-08 20:55:45

标签: javascript jquery ajax

我有以下AJAX调用:

   $.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");
        }

我认为发送参数时出错了,因为success部分未被调用。

我在这里做错了什么?

5 个答案:

答案 0 :(得分:0)

尝试将data更改为实际对象: -

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()},

答案 1 :(得分:0)

您可以尝试使用

requestLayout()

不确定是否必须删除此处的charset部分。

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

答案 2 :(得分:0)

我会像这样提前构建JSON对象。我删除了不必要的代码并将其简化了一点。如果通话中出现问题,则会出现“错误”错误。函数被调用,而不是失败'。确保您呼叫的URL有效。我假设这个脚本在aspx文件所在的同一文件夹中运行。

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);
    }
});

当然,您也可以直接在“数据”中构建一个Object Literal。 AJAX调用的节点如下:

 $.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);
    }
});

无论哪种方式都有效,第二种方式可能会稍微提高效率,因为您没有将JSON对象存储在变量中,但是我做的第一种方式使得AJAX调用更具可读性,除非内存非常我坚持(在你的情况下,我怀疑它),而不是我坚持我的第一个例子。个人偏好。

答案 3 :(得分:0)

数据对象中的双引号不匹配。在下一个属性开始之前添加结束双引号。

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() + '"}', 

答案 4 :(得分:0)

构建JSON数据对象的方式似乎存在语法问题。没有双引号来包含值,并且字符串周围没有任何双引号,请尝试重写它:

$.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");
  }
});

请参阅:http://www.json.org;)

相关问题