2016-02-08 19 views
1

我有以下的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部分不叫。

我在這裏做錯了什麼?

+0

是否'失敗:函數() {...}'火?在另一個說明中,您似乎試圖手動創建JSON以通過電線發送?您應該考慮正常構建JavaScript對象,然後在所述對象上調用'JSON.parse(obj);'來生成JSON。我也認爲jQuery在內部爲你處理這個問題,但我不確定所以不要抱着我...... – War10ck

+0

*發生了什麼?是否提出了AJAX請求?它是否包含您期望的數據?服務器的迴應是什麼? JavaScript控制檯中是否有錯誤? – David

+0

不接受對象,不是奇怪的字符串? – baao

回答

0

你可以嘗試使用

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

不知道你將不得不在這裏刪除字符集的一部分。

contentType: "application/json; charset=utf-8" 
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()}, 
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); 
    } 
}); 

當然,你也可以只建立直接的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調用更具可讀性,並且除非內存非常緊密(我懷疑它是否在你的情況下)比我更願意堅持我的第一個例子。個人喜好。

+1

請注意,dataType指的是響應數據的類型,而不是您要發送的數據。 – phenxd

+0

我只是把它留在那裏的B/C它是在他原來的代碼。是的,dataType不是必需的,但它有助於評估對正確格式的響應。如果他確信這是他回來的JSON響應,那很好,否則,最好不要這樣做。我自己,我通常會把它排除在外。接得好。 – Brett84c

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