0

I am trying to understand what the difference between these two? I have read that .getJSON calls .ajax. But what functionally is different? Which would work?

Imagine that you are trying to write a client-side webapp that interacts with the following RESTFUL API that represents an online car catalog: GET /cars - Returns a list of all cars in the catalog

Example response:
{"cars": [
    {"id": 1, "model": "Alice in Wonderland", "make": "Chevy"},
    {"id": 2, "model": "Civic", "make": "Honda"}
]}

POST /cars - Creates a new car based on the given info 

Example request:
{"model": "Wrangler", "make": "Jeep"} 

Example response:
{"id": 3} 

GET /cars/<id> - Returns info about the car with that id 

Example response:
{"id": 1, "model": "Alice in Wonderland", "make": "Chevy"} 

PUT /cars/<id> - Overrides info about the car with the given id with the new data 

Example request:
{"model": "Wrangler", "make": "Jeep"} 

DELETE /cars/<id> - Deletes the car at the given id. The id may be recycled for other cars 

You have been instructed to write some code that:

  1. Deletes all cars made by Chevy
  2. Modifies the models of all cars written by Jeep to all-caps

    $.ajax({ url: "/cars", type: "GET" }, function(data) {
            data.cars.forEach(function (car) {
                var endpoint = "/car/" + car.id;
                if (car.make == "Chevy") {
                    $.ajax({ url: endpoint, type: "DELETE" });
                } else if (car.make == "Toyota") {
                    var request = {title: car.title.toUpperCase(), make: car.make};
                    $.ajax({ url: endpoint, type: "PUT", data: JSON.stringify(request) });
                }
            });
        });
    
    $.getJSON("/cars", function(data) {
        data.cars.forEach(function (car) {
            var endpoint = "/car/" + car.id;
            if (car.make == "Chevy") {
                $.ajax({ url: endpoint, type: "DELETE" });
            } else if (car.make == "Toyota") {
                var request = {title: car.title.toUpperCase(), make: car.make};
                $.ajax({
                    url: endpoint,
                    type: "PUT",
                    data: JSON.stringify(request),
                    contentType: "application.json"
                });
            }
        });
    });
    
A. Student
  • 91
  • 8
  • 4
    http://stackoverflow.com/questions/1076013/difference-between-getjson-and-ajax-in-jquery Difference Between $.getJSON() and $.ajax() in jQuery – mattwilsn Feb 17 '16 at 04:12
  • @user1675557 Instead of leaving a comment about a duplicate, go ahead and vote to close as a duplicate (if you're sure it is). –  Feb 17 '16 at 04:51
  • Does this answer your question? [Difference Between $.getJSON() and $.ajax() in jQuery](https://stackoverflow.com/questions/1076013/difference-between-getjson-and-ajax-in-jquery) – Peyman Mohamadpour May 05 '20 at 18:20

0 Answers0