weixin_33682790 2015-05-09 18:08 采纳率: 0%
浏览 85

Ajax响应-成功/失败

I have seen multiple ways in the past of returning success/failure responses from ajax controller actions from backend services.

Is there an accepted best practice for this?

Im thinking in terms of whether the call was successful and also the transport of any error messages.

  • http codes
  • true / string (which would contain the error message)
  • json encoded object containing success/failure flag + data
  • etc

Ive seen things like the above and also 'success' / 'error' responses etc.

My specific scenario here is a controller say for example testConnectionController. It tests whether a database connection is active or has any issues and reports the status back to the client.

  • 写回答

1条回答 默认 最新

  • weixin_33725126 2015-05-09 18:15
    关注

    You should definitely follow the recommendations for http response codes. Don't be tempted to ignore setting the response code in favor of some structured json error response. Other than that it is really dependent on the needs of you application. What ever you end up going with it is helpful to be consistent across all of your responses. e.g. If you are going to return an error as json make sure every http end point returns the same structure. That way your client can react in the same way to every error response. Generally speaking there isn't much your client is going to be able to do other than display the error to the user so a simple response like this, with various messages, will cover most cases:

    {
        "success" : false,
        "message" : "Terrible things occurred!"
    }
    
    评论

报告相同问题?

悬赏问题

  • ¥15 微信会员卡等级和折扣规则
  • ¥15 微信公众平台自制会员卡可以通过收款码收款码收款进行自动积分吗
  • ¥15 随身WiFi网络灯亮但是没有网络,如何解决?
  • ¥15 gdf格式的脑电数据如何处理matlab
  • ¥20 重新写的代码替换了之后运行hbuliderx就这样了
  • ¥100 监控抖音用户作品更新可以微信公众号提醒
  • ¥15 UE5 如何可以不渲染HDRIBackdrop背景
  • ¥70 2048小游戏毕设项目
  • ¥20 mysql架构,按照姓名分表
  • ¥15 MATLAB实现区间[a,b]上的Gauss-Legendre积分