让Ajax应用知道服务器上的错误的最佳方法是什么? [英] What's the best way to let the Ajax app know of the errors back at server?

查看:96
本文介绍了让Ajax应用知道服务器上的错误的最佳方法是什么?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

嗨 我正在使用Java作为服务器端语言来开发应用程序,而对于客户端,我正在使用Ajax. 但是我对ajax应用程序还很陌生,因此我需要针对自己遇到的问题发表一些意见. 我将Spring Security用于身份验证和授权服务,并且通过阅读Spring论坛,我设法将Spring Security与Ajax应用程序集成在一起,从而可以拦截ajax请求并采取相关措施. 这是问题所在:让ajax应用程序知道服务器上已发生错误的最佳方法是什么.到目前为止,我一直在做的是,按照惯例,我会随机产生http 500+错误. e. G.提示登录,我返回550,其他问题返回551,依此类推.但是我认为这不是正确的方法.处理这种情况的最佳方法是什么?

Hi I'm working on an application with Java as it's server-side language and for the client-side I'm using Ajax. But I'm fairly new to ajax applications so I needed some opinions on the issue I've faced. I'm using Spring Security for my authentication and authorization services and by reading spring forums I've managed to integrate Spring Security with Ajax application in a way that ajax requests can be intercepted and relevant action be taken. Here's the issue: What is the best way to let the ajax application know that an error has occurred back at server. What I've been doing so far is that by convention I make random http 500+ errors. e. g. to prompt for login I return 550, and 551 for other issue and so forth. But I think this is not the right approach to this. What is the best approach for dealing with this situation?

推荐答案

如果标准的HTTP错误代码(例如401未经授权)足够丰富,请使用它们.最好不要编写您自己的HTTP错误代码,这些错误代码已得到修复.如果需要返回更多信息,则应在响应正文中返回一个更丰富的对象(序列化为JSON或XML),然后在客户端解析该对象.

If standard HTTP error codes (eg 401 Unauthorized) are rich enough, use them. Best not to make up your own HTTP error codes, they're meant to be fixed. If you need more info to be returned, you should return a richer object in the response body (serialized as eg JSON or XML) and parse the object on the client side.

这篇关于让Ajax应用知道服务器上的错误的最佳方法是什么?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
相关文章
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆