简体   繁体   中英

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). The server side is an asmx service in c#.

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:

'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.

What I want is to get the exception info in 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:

eg res.Result (as "OK" or "ERR" etc and handle that on the client)

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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