Javascriptmvc tutorial-13. Ajax Service Guide

Source: Internet
Author: User
Tags representational state transfer

Because of the flexibility of javascriptmvc, it can be developed with any service layer. The purpose of this Guide is to show you how to design the service layer to minimize the extra work required to run javascriptmvc. Generally, the lighter the service layer, the better. The database query results are returned directly to ensure the flexibility of communication with the client.

  

  JSON rest

  The best integrated service layer can be described as JSON rest. JSON is used to exchange data between the client and the server. Restful is a representational state transfer.GET POST PUTAndDelete to modify the resource URL. For example, a simple message service API exposes the following urls:

  1.GET /messages -Retrieve the message array from the server.

  2.GET /messages/{id} -Get the specified message from the server.

  3.POST /messages -Create a message on the server.

  4.PUT /messages/{id} -Update a specified message on the server.

  5.DELETE /messages/{id}-Delete a specified message on the server.

 

  Query string Parameters

Before explaining various URLs, it is necessary to quickly introduce how parameters are passed to query strings. Jquery. Model passes the parameter to $. Ajax of jquery, which uses jquery. Param for conversion. For example:

$.get('/messages',{  userId: 5,   order: ['createdAt ASC','user.name ASC'] })

Result:

GET /messages?        userId=5&        order%5B%5D=createdAt+ASC&        order%5B%5D=user.name+ASC

 

  GET/messages

The get/messages request returns all visible data to the user. The data structure is as follows:

{  "data": [      {         "id" : 1,         "fromUserId": 921,         "text": "Hello World",         "createdAt" : 1024324214123      },      {         "id" : 2,         "fromUserId": 923,         "text": "Goodnight World",         "createdAt" : 23524365346543      },      ...  ],  "count": 100 }

  1.data-Object array, with each data representing a message.

  2.count-Array size.

 

GET/messages organizes the passed parameters into connection strings in the form of name = value, for example, get/messages? Limit = 10 & offset = 20 & Order [] = createdat + DESC.

Common name/values:

  • limit-Size of returned data
  • offset-Data offset
  • order-Data Arrangement

The data is directly transmitted to the database query string.

Sometimes you want to obtain all the data of a specific project, such as all the data of user 52, the request URL should be like this:

GET /messages?fromUserId=52

The server needs to obtain all data with the condition fromuserid = 52.

  Related Data

Another problem is that for performance consideration, you need to add other data to a data set. For example, when you get a message, you also need to obtain user information. In this case, we encourage you to add the 'include 'parameter. The URL is as follows:

GET /messages?include[]=fromUser

Fromuser data is added to every message object:

{  "data": [      {         "id" : 1,         "fromUserId": 921,         "text": "Hello World",         "createdAt" : 1024324214123,         "fromUser": {           "id" : 921,           "name" : "Justin Meyer"         }      },      ...  ],  "count": 100 }

 

  GET/messages/{ID}

Obtain a piece of data from the server, which can be in the JSON format:

->{    "id" : 1,    "fromUserId": 921,    "text": "Hello World",    "createdAt" : 1024324214123}

 

  Post/messages

Create a data entry on the server. In general, it can be JSON data, a bit like the data obtained by a GET request. The difference is that it does not have the ID attribute and the attributes that will be generated by some other servers, as shown in the following example:

POST /messages{    "fromUserId": 921,    "text": "A new message"}

The server will generate the ID and createat attributes for you. The data returned by the server may be:

->{    "id": 22,    "createdAt": 1224324214123}

 

  Put/messages/{ID}

Update the data on the server, a bit like post. The data is in JSON format. However, the data only contains the attribute values to be updated, as shown below:

PUT /messages/22{  'text': "An updated EVIL message"}  

The returned value must contain the data modified or adjusted on the server. For example, the server filters out the "blocking word" and updates the updateat attribute. The returned value should be as follows:

->{  'text' : "An updated message",  'updatedAt' : 123254356346241}

However, if the server is not updated or changed, the returned value should be an empty object :{}.

 

  Delete/messages/{ID}

Delete a piece of data from the server.

 

  Sending time

The best form of sending time is to use an integer of the Julian Date type, for example:

createdAt: 12313123133423

We can easily use JavaScript to convert it to the date type:

new Date(123123133423)

 

  Use one request for multiple data records

You can use jquery. model. List to create, update, and delete multiple data using one request.

 

  Exception Handling

When an exception occurs, make sure that your server returns the correct HTTP status code. The returned object can be JSON data, and the attribute name corresponds to the exception array:

{   email : ["Formatting is incorrect","No email is provided"]}

 

Javascriptmvc tutorial directory

Related Article

Contact Us

The content source of this page is from Internet, which doesn't represent Alibaba Cloud's opinion; products and services mentioned on that page don't have any relationship with Alibaba Cloud. If the content of the page makes you feel confusing, please write us an email, we will handle the problem within 5 days after receiving your email.

If you find any instances of plagiarism from the community, please send an email to: info-contact@alibabacloud.com and provide relevant evidence. A staff member will contact you within 5 working days.

A Free Trial That Lets You Build Big!

Start building with 50+ products and up to 12 months usage for Elastic Compute Service

  • Sales Support

    1 on 1 presale consultation

  • After-Sales Support

    24/7 Technical Support 6 Free Tickets per Quarter Faster Response

  • Alibaba Cloud offers highly flexible support services tailored to meet your exact needs.