Home > Article > Web Front-end > These core knowledge points must be mastered in front-end and back-end communication!
restful is the most popular interface design specification at the moment, a specification that both front-end and back-end must know
This specification defines each process in interface development
Protocol
The client should use the HTTPS protocol when communicating with the back-end service through the API
Domain name
API should Deployment under a dedicated domain name should be kept as simple as possible. If you are sure that the API is very simple and will not be further expanded, you can consider placing it under the main domain name. Here are two common URL root examples:
Versioning
All The API must remain backward compatible. You must introduce a new version of the API while ensuring that the old version of the API is still available. Therefore, version support should be provided for it
Address (path) routing (router)
The specific URL of the API
As for whether the URL must use hyphens (-) or underscores (_), there is no hard and fast rule, but it must be unified according to the team situation. Style
Verb (request method method)
The specific operation type of the resource is represented by the HTTP verb. There are five commonly used HTTP verbs (the corresponding SQL commands are in parentheses).
url parameters
All URL parameters must be in all lowercase, and the underscore type parameter form must be used
The paging parameters must be fixed to page, page_size
Response
The appropriate HTTP status code must be selected.
Must return JSON format data.
All errors must be in the same error format.
Common status codes
Error format
'message' => ':message', // 错误的具体描述 'errors' => ':errors', // 参数的具体错误描述,422 等状态提供 'code' => ':code', // 业务自定义的异常码 'status_code' => ':status_code', // http状态码 'debug' => ':debug', // debug 信息,非生产环境提供
What is a message?
HTTP messages are used to exchange information when HTTP requests and responses. Messages flow from the client to the server.
The messages are divided into two categories: request messages and response messages.
The basic message structures of request and response messages are the same, and they are both composed of Composed of three parts:
axios
Compatible with browsers and node servers
Create XMLHttpRequests from the browser
Create http requests from node.js
Support Promise API
Support interception of requests and responses
Convert request data and response data
Cancel request
Automatically convert JSON data
Client supports defense against XSRF
Come on, stranger People! (I am no stranger to you here and there, I hope it will be helpful to you)
The above is the detailed content of These core knowledge points must be mastered in front-end and back-end communication!. For more information, please follow other related articles on the PHP Chinese website!