RESTful CRUD by JavaScript

WCF Rest Services

WCF Rest Services are normal WCF Services that have added functionality so that they can be consumed in a RESTful manner (URI vs URL, usage of HTTP Verbs, usage of different Data Transfer Formats like JSON, YAML, etc). The table below will help you to understand how the HTTP verbs are typically used to implement a web service.

Method Description
GET Requests a specific representation of a resource
PUT Creates or updates a resource with the supplied representation
DELETE Deletes the specified resource
POST Submits data to be processed by the identified resource
HEAD Similar to GET but only retrieves headers and not the body
OPTIONS Returns the methods supported by the identified resource

 

Using GET Method

List the URIs and perhaps other details of the collections. In our case, requests collection of members.

 $.ajax({
            type: "GET",
            url: "Services/EFService.svc/Members",
            data: "{}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (data) {
                // Play with returned data in JSON format
            },
            error: function (msg) {
                alert(msg);
            }
        });    

Using POST Method

Retrieve a representation of the addressed member of the collection, in the example below, create a new entry in the collection.

 $.ajax({
         type: "POST",
         url: "Services/EFService.svc/Members/",
         data: "{Email:'[email protected]', ScreenName:'TestUser'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (data) {
           // Play with response returned in JSON format
         },
         error: function (msg) {
             alert(msg);
        }
    });    

Using PUT Method

Update the entire collection with another collection, in the example below, update the addressed member of the collection.

$.ajax({
         type: "PUT",
         url: "Services/EFService.svc/Members/",
         data: "{Email:'[email protected]', ScreenName:'TestUser'}",
         contentType: "application/json; charset=utf-8",
         dataType: "json",
         success: function (data) {
           // Play with response returned in JSON format
         },
         error: function (msg) {
             alert(msg);
         }
     });

Using DELETE Method

Delete the entire collection or a specific collection, in the example below, delete Member with id=1.

 $.ajax({
           type: "DELETE",
           url:  "Services/EFService.svc/Members(1)",
           data: "{}",
           contentType: "application/json; charset=utf-8",
           dataType: "json",
           success: function (data) {
               // Play with response returned in JSON format
           },
           error: function (msg) {
               alert(msg);
           }
       });    


来源:http://www.codeproject.com/Articles/254714/Implement-CRUD-operations-using-RESTful-WCF-Servic

About 智足者富

http://chenpeng.info

发表评论

电子邮件地址不会被公开。 必填项已用*标注

You may use these HTML tags and attributes:

<a href="" title=""> <abbr title=""> <acronym title=""> <b> <blockquote cite=""> <cite> <code> <del datetime=""> <em> <i> <q cite=""> <s> <strike> <strong>