【发布时间】:2015-04-30 13:46:03
【问题描述】:
我有一个从 ajax 调用的 WebMethod - 如果抛出错误,如果类型不是字符串/int 等,我如何将它返回给 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 能够处理异常 ex:
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
无法将类型“System.Net.HttpStatusCode”隐式转换为“System.Collections.Generic.List”
catch (WebException ex)
{
var statusCode = ((HttpWebResponse)ex.Response).StatusCode;
return statusCode;
}
【问题讨论】: