Permalink
Browse files

Updated request docs to return errors fixes #6

  • Loading branch information...
1 parent 85c30dd commit 4d8de26866991db1baa2f356f0d41b45a9348051 @cblanc cblanc committed Feb 27, 2014
Showing with 58 additions and 18 deletions.
  1. +58 −18 public/js/app.js
View
@@ -3,38 +3,58 @@ $(function () {
$(this).val("");
});
+ var displayJsonResult = function ($context, data) {
+ $context.html(JSON.stringify(data, null, 4)).slideDown();
+ }
+
$("#single-postcode").click(function (event) {
event.preventDefault();
var $result = $("#single-postcode-result").slideUp(),
postcode = $("#single-postcode-input").val();
- $.get(encodeURI("/postcodes/" + postcode), function (data) {
- $("#single-postcode-result").html(JSON.stringify(data, null, 4)).slideDown();
+ $.get(encodeURI("/postcodes/" + postcode))
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
});
$("#random-postcode").click(function (event) {
event.preventDefault();
var $result = $("#random-postcode-result").slideUp();
- $.get(encodeURI("/random/postcodes"), function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.get(encodeURI("/random/postcodes"))
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
});
$("#validate-postcode").click(function (event) {
event.preventDefault();
var $result = $("#validate-postcode-result").slideUp(),
postcode = $("#validate-postcode-input").val();
- $.get(encodeURI("/postcodes/" + postcode + "/validate"), function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.get(encodeURI("/postcodes/" + postcode + "/validate"))
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
});
$("#autocomplete-postcode").click(function (event) {
event.preventDefault();
var $result = $("#autocomplete-postcode-result").slideUp(),
postcode = $("#autocomplete-postcode-input").val();
- $.get(encodeURI("/postcodes/" + postcode +"/autocomplete"), function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.get(encodeURI("/postcodes/" + postcode +"/autocomplete"))
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
});
@@ -43,17 +63,25 @@ $(function () {
var $result = $("#geocode-postcode-result"),
lon = $("#lon-input").val(),
lat = $("#lat-input").val();
- $.get(encodeURI("postcodes/lon/" + lon +"/lat/" + lat), function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.get(encodeURI("postcodes/lon/" + lon +"/lat/" + lat))
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
});
$("#query-postcode").click(function (event) {
event.preventDefault();
var $result = $("#query-postcode-result"),
postcode = $("#query-postcode-input").val();
- $.get(encodeURI("/postcodes?q=" + postcode), function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.get(encodeURI("/postcodes?q=" + postcode))
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
});
@@ -83,8 +111,12 @@ $(function () {
try {
postcodes = JSON.parse(data);
- $.post("/postcodes", postcodes, function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.post("/postcodes", postcodes)
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
} catch (e) {
$bulkPostcodeInput.addClass("invalid");
@@ -117,8 +149,12 @@ $(function () {
try {
locations = JSON.parse(data);
- $.post("/postcodes", locations, function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.post("/postcodes", locations)
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
} catch (e) {
$bulkGeocodeInput.addClass("invalid");
@@ -129,8 +165,12 @@ $(function () {
event.preventDefault();
var $result = $("#show-outcode-result").slideUp(),
outcode = $("#show-outcode-input").val();
- $.get(encodeURI("/outcodes/" + outcode), function (data) {
- $result.html(JSON.stringify(data, null, 4)).slideDown();
+ $.get(encodeURI("/outcodes/" + outcode))
+ .done(function (data) {
+ displayJsonResult($result, data);
+ })
+ .fail(function (error) {
+ displayJsonResult($result, error.responseJSON);
});
});
});

0 comments on commit 4d8de26

Please sign in to comment.