Use the JQuery post Json object to access the WCF restful service, jqueryjson
1. Create a DataContract class: Book.
2. Create the IBookService Interface related to the BoOK class and the BookService class that implements the interface.
3. Configure and deploy Restful WCF service.
We will add a method in the IBookService Interface
[OperationContract]
[WebInvoke (Method = "POST", RequestFormat = WebMessageFormat. Json, ResponseFormat = WebMessageFormat. Json,
UriTemplate = "SaveBook")]
String SaveBook (Book book );
Override the SaveBook method in BookService
Public string SaveBook (Book book)
{
// Call this method to save the library object to the database
// MyBookImp. SaveBook (book)
Return "id =" + book. bId;
}
Finally, use JQuery to call the SaveBook method of WCF.
Function SaveBook (){
Var bookData = {
"BName": "32334833425543 ",
"BCount": "32"
};
$. Ajax ({
Type: "POST ",
Url: "http: // localhost/MyBookService/BookService. svc/SaveBook ",
Data: JSON. stringify (bookData ),
ContentType: "application/json; charset = UTF-8 ",
DataType: "json ",
ProcessData: true,
Success: function (data, status, jqXHR ){
Alert ("success ..." + Data );
},
Error: function (xhr ){
Alert (xhr. responseText );
}
});
}