简体   繁体   English

如何处理JavaScript Web应用程序在C#中引发的异常?

[英]How to handle a exception thrown in c# by a javascript web app?

I'm developing a web application with a client side build with html5 and javascript using, overall, dojo framework (I'm using xhr.post function to communicate with server). 我正在开发一个Web应用程序,并使用整体dojo框架(带有html5和javascript)进行客户端构建(我正在使用xhr.post函数与服务器进行通信)。 The server side is an asmx service in c#. 服务器端是c#中的asmx服务。

Here comes the problem: when a webmethod of my service throws an exception: 问题来了:当我的服务的网络方法引发异常时:

        try
        {
            throw new Exception("TEST");
        }
        catch (Exception ex)
        {
            throw ex;
            // TODO: send some info to the client about the error
        }

the client is realized than an error has occured and fires the error callback function: 意识到客户端已发生错误,并触发错误回调函数:

        deferred = xhr.post({
            url: that.config.urlService,
            handleAs: that.config.handleAs,
            contentType: that.config.contentType,
            postData: that.config.parameters
        });

        deferred.then(function (res) {
            that.success(res);
        },
            function (err) {
                 if (that.config.callbackFail) {
                     that.config.callbackFail(err, that.config.parameters);
                 }
            }
        );

But in the 'err' paramater I don't have the exception info, I have the message: 但是在'err'参数中,我没有异常信息,但有消息:

'NetworkError: 500 Internal Server Error - http:/.../Services/Applications/Metrawa/ServiceManagement/WSJob.asmx/Filter' 'NetworkError:500 Internal Server Error-http:/.../ Services / Applications / Metrawa / ServiceManagement / WSJob.asmx / Filter'

and inspecting the call to server with firebug, in the response tab I can see strange characters, like if the exception hasn't been serialized. 并使用firebug检查对服务器的调用,在“响应”选项卡中,我可以看到奇怪的字符,例如未序列化异常。

What I want is to get the exception info in javascript. 我想要的是获取javascript中的异常信息。 I've searched some info about this, like to create an own serializable exception class, but it doesn´t work neither. 我已经搜索了一些与此相关的信息,例如创建了一个自己的可序列化的异常类,但是它都不起作用。 Any solution? 有什么办法吗?

Thanks in advance. 提前致谢。

您可以选择在服务器端代码中处理该异常,然后以JSON,XML甚至String的形式发送回该异常,即客户端可以理解的内容。

I would catch the exception server side and always return a valid object with a "Result" property: 我将捕获异常服务器端,并始终返回带有“ Result”属性的有效对象:

eg res.Result (as "OK" or "ERR" etc and handle that on the client) 例如res.Result(如“ OK”或“ ERR”等,并在客户端上处理)

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM