开发者

Use custom status code or render custom text for ajax?

开发者 https://www.devze.com 2023-04-13 03:20 出处:网络
which is a better approach? a custom status code or render a text for ajax render :text => \"pusher\"

which is a better approach? a custom status code or render a text for ajax

render :text => "pusher"

or

render :nothing => true , :status => 900

for

$.ajax({
  success : fu开发者_StackOverflow社区nction(d , s , r){
    if (d == "pusher"){
    }
  }
});


If you want to indicate success then you must return a 2XX status code or you're not speaking HTTP. If you're not return a valid status code then you can't expect the clients do anything useful with your response.

So you have to return one of the 2XX status codes so you're left with:

render :text => 'pusher'

as your only viable solution.


best practice is to use http status for these kind of stuff.

But it will only work with the http state code. So if you have a response related to business logic and not to 2xx or 4xx status, you will need your render :text => "pusher" approch (as "pusher" is not an http state).

In my apps, I prefer write an API and link some standart response to my business logic.

0

精彩评论

暂无评论...
验证码 换一张
取 消