Skip to main content

Javascript REST client

Note: I still work on text, code example should be fine.



REST is the one of the most popular interfaces on the web today. One part to its success it owes to its simplicity.

How number of sites that support REST grows we will need some fast and good solution to use those services on client side. Even if its possible to do it all, since REST is based on http, with old AJAX calls, it would be nice to have some little more...

This is one approach that i choose. I create in javascript new object that has six methods five for REST methods, four for POST, PUT, GET and REMOVE plus one more to GET all resources, and one more to get html template to display your data.

This code is based on jquery and json js libs.

function RestServiceJs(newurl) {
  this.myurl = newurl;

  this.add = function(model, callback) {
    $.ajax({
      type: 'POST',
      url: this.myurl,
      data: JSON.stringify(model), // '{"name":"' + model.name + '"}',
      dataType: 'text',
      processData: false,
      contentType: 'application/json',
      success: callback,
      error: function(req, status, ex) {},
      timeout:60000
    });
  };

  this.update = function(model, callback) {
    $.ajax({
      type: 'PUT',
      url: this.myurl,
      data: JSON.stringify(model), // '{"name":"' + model.name + '"}',
      dataType: 'text',
      processData: false,
      contentType: 'application/json',
      success: callback,
      error: function(req, status, ex) {},
      timeout:60000
    });
  };

  this.find = function(id, callback) {
    $.ajax({
      type: 'GET',
      url: this.myurl + '/' + id,
      contentType: 'application/json',
      success: callback,
      error: function(req, status, ex) {},
      timeout:60000
    });
  };

  this.findAll = function(callback) {
    $.ajax({
      type: 'GET',
      url: this.myurl,
      contentType: 'application/json',
      success: callback,
      error: function(req, status, ex) {},
      timeout:60000
    });
  };

  this.remove = function(id, callback) {
    $.ajax({
      type: 'DELETE',
      url: this.myurl + '/' + id,
      contentType: 'application/json',
      success: callback,
      error: function(req, status, ex) {},
      timeout:60000
    });
  };

  this.loadTmpl = function(turl, callback) {
    $.ajax({
      url: turl,
      success: callback,
      error: function(req, status, ex) {},
      timeout:60000
    });
  }
}


Later you will just need to extend your rest service with your url and use it.
After that you can use in your code like this:

var carRestServiceJs = new RestServiceJs('/rest/car');

// add update event
$("a.update_link").click(function() {

  carRestServiceJs.find(this.id, function(data) {
  
    loadAddModel(data);
    $("#modelName").val(data.name);
  });
});


Please tell me what do you think about it, and,,,,

Comments

Anonymous said…
Thanks for your article, it's very useful !
However I think you've inverted two ajax parameters : contentType and dataType.
- contentType : "When sending data to the server, use this content-type."
- dataType : "The type of data that you're expecting back from the server".

So when you're making a GET/DELETE request, you should specify dataType : 'json' or 'html' and not contenType : 'application/json' as you're not sending data to the server.
Anonymous said…
https://github.com/jpillora/jquery.rest :)
Anna Schafer said…
This tutorial will turn out to be useful for those who are just starting to learn Blogger template and plugin development. In this article, we will show you How to Correctly Add JavaScript codes in Blogger?psd to css
Unknown said…
Urgent:

can you send me the full code please ?

Is this code works same like the chrome advance rest client extention ?

please provide me reference where I can learn to make rest client
Balaji said…
This comment has been removed by the author.
service care said…
Amazing article. Your blog helped me to improve myself in many ways thanks for sharing this kind of wonderful informative blogs in live. I have bookmarked more article from this website. Such a nice blog you are providing
lg mobile service center in chennai
lg mobile service center
lg mobile service chennai
Priyanka said…
Attend The Python training in bangalore From ExcelR. Practical Python training in bangalore Sessions With Assured Placement Support From Experienced Faculty. ExcelR Offers The Python training in bangalore.
python training in bangalore
saketh said…

I feel very grateful that I read this. It is very helpful and very informative and I Python classes in pune really learned a lot from it.
Anu said…
Thanks For sharing Your information The information shared Is Very Valuable Please Keep Updating Us Time Just went On reading The article.
devops training in chennai | devops training in anna nagar | devops training in omr | devops training in porur | devops training in tambaram | devops training in velachery
This comment has been removed by the author.
milka said…
Great post. keep sharing such a worthy information.
Data Science Training in Chennai
Aaron said…
This software is useful to update the drivers. Every user knows that without drivers the operating system can't operate any program so Driver Toolkit 8.6 Crack
haseeb said…
Private Messenger 5.96.2 (x86) (Android 5.0+) APK Download by Wickr Inc - APKMirror Free and safe Android APK downloads.Wickr Me Apk
Vijayitguru said…
This blog is very helpful for learners planning to start mongodb classes with practical database skills.
Vijayitguru said…
A well-written article for beginners looking for a structured mongodb course.
Vijayitguru said…
The content is ideal for anyone searching for a complete mongodb full course with real-time use cases.
Vijayitguru said…
This post clearly explains a beginner-friendly mongodb learning path for career growth.
Vijayitguru said…
Very informative blog for professionals planning to join mongodb training.
Vijayitguru said…
A useful guide for learners preferring flexible mongodb training online.
Vijayitguru said…
This article is a great resource for choosing the right mongodb online course.
Vijayitguru said…
Helpful content for aspirants searching for the best mongodb online course.
Vijayitguru said…
The certification details shared make this mongodb certification course very valuable.
Vijayitguru said…
A must-read blog for anyone aiming to enroll in the best mongodb course.
Vijayitguru said…
This post clearly highlights the importance of a professional mongodb administration course.
Vijayitguru said…
Very useful article for database professionals interested in a mongodb dba course.
Vijayitguru said…
A great guide for beginners who want to mongodb learn online step by step.
Vijayitguru said…
This blog is especially helpful for learners planning to join mongodb dba online training.
Vijayitguru said…
An informative post for anyone searching for flexible mongodb online classes.