Ica*_*rus 3 asp.net jquery json web-services asmx
我们有一个ASMX Web服务,我们使用ajax(jQuery)从我们的ASP.NET应用程序调用它.
我们的Web方法的典型示例如下:
[WebMethod]
public void DoSomething(BusinessObject myParameter)
{
try
{
BL.DoSomethingWithParam(myParameter);
}
catch(Exception ex)
{
//logic to log the actual exception goes here
//and then we throw a more user-friendly error as so:
throw new Exception("Unable to perform action such an such");
}
}
Run Code Online (Sandbox Code Playgroud)
在客户端,我们会有这样的事情:
$.ajax({
type: "POST",
url: "WebService.asmx/DoSomething",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(result) {
//do something with result.d
},
error: function(xhr, ajaxOptions, thrownError){
alert($.parseJSON(xhr.Response.Text).Message);
}
});
Run Code Online (Sandbox Code Playgroud)
上面的方法有几个问题,我想解决这个问题:
"There has been an error processing your request." 总之,我们没有适当地处理这个问题,所以我的问题是:
There has been an error processing your request而远程测试IE中断,但为什么Firefox不会这样做呢?我们实现的一个替代方案 - 现在,我们仍然处于开发阶段 - 只是在发生错误时从我们的Web方法返回一个字符串,但这实际上是一种丑陋的黑客/不优雅的方式.
注意:不要问我"处理您的请求时出错"消息来自何处.我没有最微弱的想法.我们的代码中没有任何可以返回该消息的内容.
我不知道是否已经出现了一种"正确"的方式,因为Web服务的使用方式正在如此迅速地变化,并且最终构建为使用服务的任何客户端都能够处理您选择的任何方法.我毫不怀疑最终会设计一个,但现在由你决定.
据说这是为了避免我过去看到的一些更常见的陷阱.
我通常从这些日子开始的方法看起来像这样:
{
Success: bool,
Errors[]: {
Id: string,
Source: string,
Message: string
},
Message: { *Method specific structure* }
}
Run Code Online (Sandbox Code Playgroud)