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:
- Deletes all cars made by Chevy
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" }); } }); });