Author Archives: Junaid Ali Qureshi

7 Tip to Post JSON data to WCF RESTful Service using jQuery

JSON

When it comes to web development, you need to be sure you have picked the right program to develop the web, application or the software. There are hundreds of things that can go wrong. From the code to the arrangement of the functions and other related tasks; all must be done with detailed care. The JSON or JavaScript Object Notation is a readable format for structuring data. With the help of this format, you can transmit data between the web application and the server. Along with the POST request method, you can post the data on the web server for the purpose of storage. Many developers look forward to Post JSON data to WCF RESTful service using the jQuery language as the WCF RESTful service platform offers great benefits.

Similarly, the windows communication foundation is a scalable, reliable and a secure messaging platform. It is widely used to build the Web services in .NET. With this unified programming model approach you can develop a service-oriented application. Hire Magento developer and he would be aware of the complexities and features of jQuery and JavaScript. With his help, you can do the above-mentioned task easily.

You can also checkout Free MOOC Courses from Top Universities while interacting with students world-wide and under guidance of top professors.

Here are the tips to use while you Post JSON data to WCF RESTful service:

1. Understand the HTTP verb and their implementation

In the field of programming, it is very important to know what you are dealing with. The HTTP verbs are used readily in the world of programming and ‘POST’ in the ‘Post JSON’ is one of the verbs. So, do you know what the HTTP verbs are? They are the main part of the ‘uniform interface’ constraint. They are actually the action counterpart to the noun-based resource. POST, GET, PUT, DELETE, and PATCH are the popular examples of these verbs also known as the methods. The verb that you have to deal is POST and it stands for ‘submit data to a resource’.  With this information at hand now you can move forward and carry out the task.

2. Create a new resource using POST HTTP verb

This task often takes a complete tutorial for a detailed explanation but we will manage to give you the major chunks of the task here. It is one of the essential first steps that the programmer has to take to complete the job. The tip here is that instead of spending your time on going through the complete method of generating the complete code for making a new resource with the help of POST HTTP verb you can use the following short version to recall the steps:

  • The DataContract class i.e. “Book” is there
  • A class BookRepository as an interface for IBookRepository is there
  • The RESTful service BookService is for implementing the IBookService
  • At last is the configuration related to the service

3. Use the service method properly

The first step often requires adding service method “SaveBook” to the already present BookService. For IBookService interface, here is the method that you can use for easy handling of the program:

[OperationContract]

[WebInvoke(Method = "POST",

RequestFormat = WebMessageFormat.Json,

ResponseFormat = WebMessageFormat.Json,

UriTemplate = "SaveBook/{id}")]

string SaveBook(Book book, string id);

Here:

Method is the HTTP verb i.e. POST

RequestFormat is used to define the request message format i.e. JSON according to our example

ResponseFormat shows the response message format i.e. also JSON here

UriTemplate is for the unique URI for this service operation.

4. Try not to stress over the configuration settings for Bookservice.cs

The above-mentioned method is implemented in the BookService.cs as follows:

public string SaveBook(Book book, string id)

{

Book newBook = repository.AddNewBook(book);

return "id=" + newBook.BookId;

}

Now, once you have reached this point, a lot of developers wonder about the configuration settings for the BookService. The thing is that it will remain the same. No operation-specific configuration is required for this purpose.

5. Make sure you don’t mess up with the last step

The last step as it is tricky is the arena where you might get confused. The simple thing to do is follow the code we have mentioned below. All you have to do is consume the service using jQuery and then call SaveBook service operation with the help of jQuery Ajax POST. Here is how you do that:

function SaveBook() {
 var bookData = {
 "BookId": 0,
 "ISBN": "32334833425543",
 "Title": "WCF RESTful Service by Example"
 };
 $.ajax({
 type: "POST",
 url: "http://localhost/RESTServiceCRUD/BookService.svc/SaveBook/0",
 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);
 }
 });
}

6. Addition of new book to the repository – Are you on the right track?

If you have followed the above-mentioned tips, we are pretty sure you are safe but to recheck, here are a few things that you need to keep in mind:

  • The URL points to the BookService.svc plus “SaveBook/0”. During the process of defining the service operation, we do it as “UriTemplate” in WebInvoke attribute.
  • BookData being a JSON object, can easily be passed as a parameter to the data element.
  • The 0 here is for an id parameter so that we can add a new book
  • With this, our query is solved. Yes, the addition of the new book to the repository is where your task ends.

7. Double check your coding method and make sure you are doing it right

The programming activity is a tough one and it requires a lot of practice. Before you get all messed up and wonder whether you have taken all the steps right just take a break and go through all the steps again. It is highly likely that you might have entered a code wrong or skipped a step.

With this, we end our guide on the tips to Post JSON to WCF RESTful service using the famous jQuery. We hope your process goes by without a problem!

Author Bio:

Junaid Ali Qureshi is leader/representative/frontrunner of an expert magento development team and an experienced digital marketing specialist dedicated to develop intuitive, well crafted, smart websites having blistering opening on search engine(s) making time and money worthwhile. His current ventures include magentodevelopers.online ,Elephantation, eLabelz, Smart Leads.ae, Progos Tech and eCig.