I'm relatively new to Javascript and I'm stuck trying to obtain a JSON data from an URL using AJAX.
The url returns an array on characters that I want to request/obtain and then handle the data to show it in html. That url is: http://stark-tundra-9859.herokuapp.com/locations
The code that I'm using is the following, and the problem is that it appears as if I received nothing for response. Besides I dont know what the request info variable should be:
function ajax_request() {
requestInfo='';
var params = JSON.stringify(requestInfo);
$.ajax({
type: "GET",
url: 'http://stark-tundra-9859.herokuapp.com/locations',
data: params,
contentType: "application/json",
dataType: "json",
converters: {
'text json': true
},
success: function(response) {
$("#responseParagraph").html(response);
},
error: function(error) {
$("#responseParagraph").html(error.responseText);
}
});
}
@agam360, I also have done a version of this code using JQUERY and I do receive a message in the console which goes as follows:
GET http://stark-tundra-9859.herokuapp.com/locations 200 OK 198ms
Response header Connection keep-alive Content-Length 154 Content-Type application/json;charset=utf-8 Server thin 1.5.1 codename Straight Razor X-Content-Type-Options nosniff
Request header Accept application/json, text/javascript, /; q=0.01 Accept-Encoding gzip, deflate Accept-Language es-ES,es;q=0.8,en-US;q=0.5,en;q=0.3 Connection keep-alive Host stark-tundra-9859.herokuapp.com Origin null User-Agent Mozilla/5.0 (Windows NT 6.1; rv:16.0) Gecko/20100101 Firefox/16.0
The code used to receive that answer is the following:
function json_request() {
$.getJSON(url,
function(data) {
alert(data);
$('#responseParagraph').append("<p>"+data.responseMessage+"</p>");
});
}
In this JQUERY very It seems as if I dont receive the DATA from the JSON request correctly. Maybe I am handling it erronously?
I would greatly appreciate any help in advance!