有没有什么方法可以在我的jQuery AJAX错误消息中显示自定义异常消息作为警报?
例如,如果我想通过Struts by 在服务器端抛出异常throw new ApplicationException("User name already exists");
,我想在jQuery AJAX错误消息中捕获此消息('用户名已存在').
jQuery("#save").click(function () {
if (jQuery('#form').jVal()) {
jQuery.ajax({
type: "POST",
url: "saveuser.do",
dataType: "html",
data: "userId=" + encodeURIComponent(trim(document.forms[0].userId.value)),
success: function (response) {
jQuery("#usergrid").trigger("reloadGrid");
clear();
alert("Details saved successfully!!!");
},
error: function (xhr, ajaxOptions, thrownError) {
alert(xhr.status);
alert(thrownError);
}
});
}
});
Run Code Online (Sandbox Code Playgroud)
在第二个警报,我警告抛出的错误,我得到undefined
,状态代码是500.
我不确定我哪里出错了.我该怎么做才能解决这个问题?
我想要做的是改变C#方法在调用时的执行方式,这样我就可以编写如下内容:
[Distributed]
public DTask<bool> Solve(int n, DEvent<bool> callback)
{
for (int m = 2; m < n - 1; m += 1)
if (m % n == 0)
return false;
return true;
}
Run Code Online (Sandbox Code Playgroud)
在运行时,我需要能够分析具有Distributed属性(我已经可以做)的方法,然后在函数体执行之前和函数返回之后插入代码.更重要的是,我需要能够在不修改调用Solve的代码的情况下或在函数的开头修改代码(在编译时;在运行时这样做是目标).
目前我尝试了这段代码(假设t是Solve存储的类型,m是Solve的MethodInfo):
private void WrapMethod(Type t, MethodInfo m)
{
// Generate ILasm for delegate.
byte[] il = typeof(Dpm).GetMethod("ReplacedSolve").GetMethodBody().GetILAsByteArray();
// Pin the bytes in the garbage collection.
GCHandle h = GCHandle.Alloc((object)il, GCHandleType.Pinned);
IntPtr addr = h.AddrOfPinnedObject();
int size = il.Length;
// Swap the method.
MethodRental.SwapMethodBody(t, m.MetadataToken, …
Run Code Online (Sandbox Code Playgroud) 如何在装饰的aspx页面方法中返回错误WebMethod
?
示例代码
$.ajax({
type: "POST",
url: "./Default.aspx/GetData",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: AjaxSucceeded,
error: AjaxFailed
});
[WebMethod]
public static string GetData()
{
}
Run Code Online (Sandbox Code Playgroud)
如何从web方法返回错误?因此,可以使用jquery错误部分来显示错误详细信息.
我的* .aspx站点中有大约50多个AJAX WebMethod,它由JQuery调用(有时是原始的jquery,有时是在lib的基础上)。
这里有一些例子:
[WebMethod]
public static string GetLog()
{
DAL.LogService log = new DAL.LogService();
string items = log.GetLog();
return items;
}
[WebMethod]
public static void ClearBenchmarks()
{
DAL.LogService log = new DAL.LogService();
log.ClearBenchmarks();
}
[WebMethod]
public static void WriteLog(string message1, string message2, string user, string type)
{
DAL.LogService.WriteLog(message1, message2, user, type);
}
Run Code Online (Sandbox Code Playgroud)
实际上,在大多数时候,它们仅重定向到我的应用程序的dataLayer,但是实际上,ofc,我有50多个单一方法...
现在,我想在我的应用程序中包括错误处理-如何在不尝试捕获每种方法的情况下进行全局处理?