Last active
September 3, 2017 13:36
-
-
Save NSLog0/8418402c1a766d647914fb60f401dafe to your computer and use it in GitHub Desktop.
http status code article expiation
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| code = { | |
| success: 200, | |
| bad_request: 400, | |
| unauthorized: 401, | |
| forbidden: 403, | |
| not_found: 404, | |
| internal_server_error: 500, | |
| bad_gateway: 502, | |
| service_unavailable: 503 | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.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 | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.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); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.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'); | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| $.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 | |
| } | |
| }); |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| render status: 200, json: { response: { data: data }, success: true } | |
| render status: 400, json: { response: { data: data }, success: false } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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 |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| 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