Skip to content

Instantly share code, notes, and snippets.

@NSLog0
Last active September 3, 2017 13:36
Show Gist options
  • Select an option

  • Save NSLog0/8418402c1a766d647914fb60f401dafe to your computer and use it in GitHub Desktop.

Select an option

Save NSLog0/8418402c1a766d647914fb60f401dafe to your computer and use it in GitHub Desktop.
http status code article expiation
class CommentsController < ApplicationController
# v3 example
def show
#oop I forget add '#' to comment at line 6 then server will thown error 500
check paramter is exist
if !params[:id].present?
render json: { response: { data: nil }, success: false} and return
end
comment = Comment.find_by(id: params[:id])
render json: { response: { data: comment }, success: true}
end
end
code = {
success: 200,
bad_request: 400,
unauthorized: 401,
forbidden: 403,
not_found: 404,
internal_server_error: 500,
bad_gateway: 502,
service_unavailable: 503
}
$.ajax({ url: '127.0.0.1',
dataType: 'json',
type: "POST",
data: data,
success: function(data, textStatus, xhr) {
// do something
},
error: function(xhr, textStatus, errorThrown) {
// do something
}
});
$.ajax({ url: '127.0.0.1/aritcles/1',
dataType: 'json',
type: "GET",
success: function(data, textStatus, xhr) {
// this line will show true or false depen on server response
alert(data.success);
},
error: function(xhr, textStatus, errorThrown) {
// this line no longer user beacase ervery response is 200
// data.success gose to undefined
alert(data.success);
}
});
$.ajax({ url: '127.0.0.1/comments/1',
dataType: 'json',
type: "GET",
success: function(data, textStatus, xhr) {
// this line will show true or false depen on server response
if(data.sucess) {
// do someting on webpage
} else {
alert('comment ID is missing, please check you data');
// do process or error handle code gose here
}
},
error: function(xhr, textStatus, errorThrown) {
// this line will work because server thown 500 and line 9 not working
alert('comment ID is missing, please check you data');
}
});
$.ajax({ url: '127.0.0.1/reviews/1',
dataType: 'json',
type: "GET",
success: function(data, textStatus, xhr) {
// do something in web page
add_review_to_html(data);
},
error: function(xhr, textStatus, errorThrown) {
alert('review ID is missing, please check you data');
// other handle
}
});
class PostsController < ApplicationController
# v2 example
def show
# check paramter is exist
if !params[:id].present?
render json: { response: { data: nil }, success: false} and return
end
post = Post.find_by(id: params[:id])
render json: { response: { data: post }, success: true}
end
end
render status: 200, json: { response: { data: data }, success: true }
render status: 400, json: { response: { data: data }, success: false }
class ReviewsController < ApplicationController
# v4 example
def show
# check paramter is exist
if !params[:id].present?
render status: 400, json: { response: { data: nil }, success: false} and return
end
review = Review.find_by(id: params[:id])
render status: 200, json: { response: { data: review }, success: true}
end
end
class UsersController < ApplicationController
# v1 example
def index
user = User.all
render status: 200, json: { response: { data: user }, success: true}
end
end
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment