{"id":299,"date":"2014-11-05T13:21:24","date_gmt":"2014-11-05T17:21:24","guid":{"rendered":"http:\/\/www.joebillman.com\/blog\/?p=299"},"modified":"2014-11-05T13:21:24","modified_gmt":"2014-11-05T17:21:24","slug":"restful-calls-via-javascript","status":"publish","type":"post","link":"https:\/\/www.joebillman.com\/blog\/?p=299","title":{"rendered":"RESTful calls via JavaScript"},"content":{"rendered":"<p>I&#8217;ve been doing a lot more front-end work with JavaScript lately and have needed to make calls to RESTful APIs. One of my complaints with JavaScript is that it feels like there is not a standard way of doing things. One can always do things the plain old JavaScript way or one can use one of the many frameworks that exist. This post focuses on the plain vanilla way to use JavaScript to access a RESTful web service API. The following is a sample\u00a0function that makes a POST call:<br \/>\n<\/p>\n<pre style=\"background-color:#e5e5e5;\">RESTService.prototype.doPost = function(url, data, successHandler, errorHandler)\n{\n    var http = new XMLHttpRequest();\n\n    http.open(\"POST\", url, true);\n    http.setRequestHeader(\"accept\", \"application\/json\");\n    http.setRequestHeader(\"content-type\", \"application\/json;charset=UTF-8\");\n\n    http.onreadystatechange = function()\n    {\n        if(http.readyState == 4)\n\t{\n\t    if(http.status == 200)\n\t    {\n                successHandler({});\n\t    }\n            else\n            {\n                errorHandler({});\n            }\n\t}\n    };\n    http.send(JSON.stringify(data));\n};\n<\/pre>\n<p>\nTo change what type of call just change the &#8220;POST&#8221; to &#8220;GET&#8221; etc.<\/p>\n","protected":false},"excerpt":{"rendered":"<p>I&#8217;ve been doing a lot more front-end work with JavaScript lately and have needed to make calls to RESTful APIs. One of my complaints with JavaScript is that it feels like there is not a standard way of doing things. One can always do things the plain old JavaScript way or one can use one &hellip; <a href=\"https:\/\/www.joebillman.com\/blog\/?p=299\" class=\"more-link\">Continue reading<span class=\"screen-reader-text\"> &#8220;RESTful calls via JavaScript&#8221;<\/span><\/a><\/p>\n","protected":false},"author":1,"featured_media":0,"comment_status":"open","ping_status":"open","sticky":false,"template":"","format":"standard","meta":{"footnotes":""},"categories":[20],"tags":[83],"class_list":["post-299","post","type-post","status-publish","format-standard","hentry","category-javascript","tag-javascript-2"],"_links":{"self":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/299","targetHints":{"allow":["GET"]}}],"collection":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts"}],"about":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/types\/post"}],"author":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/users\/1"}],"replies":[{"embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcomments&post=299"}],"version-history":[{"count":0,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=\/wp\/v2\/posts\/299\/revisions"}],"wp:attachment":[{"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fmedia&parent=299"}],"wp:term":[{"taxonomy":"category","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Fcategories&post=299"},{"taxonomy":"post_tag","embeddable":true,"href":"https:\/\/www.joebillman.com\/blog\/index.php?rest_route=%2Fwp%2Fv2%2Ftags&post=299"}],"curies":[{"name":"wp","href":"https:\/\/api.w.org\/{rel}","templated":true}]}}