我有一個WebMethod,我從ajax調用 - 如果發生錯誤,如果該類型不是字符串/ int等,如何返回到ajax請求?如何獲取錯誤回ajax請求
[WebMethod]
public static List<SchedulerResource> getResourcesOnCall(Guid instructionID, bool ignorePostcode, int pageIndex, int pageSize)
{
int totalRows = 0;
List<SchedulerResource> schedulerResourceDS = null;
try
{
schedulerResourceDS = NewJobBLL.GetResourcesOnCall(instructionID, ignorePostcode, pageIndex, pageSize, ref totalRows);
return schedulerResourceDS;
}
catch (Exception ex)
{
// what do I send back here?
}
return schedulerResourceDS;
}
這是我的AJAX - 我想.fail到能夠處理該異常前:
var requestResource = $.ajax({
type: "POST",
url: "NewJob.aspx/getResourcesDayShift",
data: JSON.stringify(objResource),
contentType: "application/json; charset=utf-8",
dataType: "json"
});
requestResource.done(function (data) {
if (data.d.length == 0) {
$("#lblEngineersDayShift").text("There are no Engineers available!");
}
else {
loadTableDS(data);
}
});
requestResource.fail(function (jqXHR, textStatus, errorThrown) {
alert('loading of Engineers failed!' + jqXHR.responseText);
});
編輯:我不認爲這是重複的 - 我問如何如果它的類型爲List < SchedulerResource>,則從我的WebMethod返回statusCode(int)或statusText(string)。因此,我得到以下錯誤:
Cannot implicitly convert type 'System.Net.HttpStatusCode' to 'System.Collections.Generic.List'
catch (WebException ex)
{
var statusCode = ((HttpWebResponse)ex.Response).StatusCode;
return statusCode;
}