Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

jQuery simple getJSON doesn't work

Tags:

json

jquery

I want to load a simple json file and display it's content but it doesn't work, I don't know why :(

Nothing is displayed on console.

<!DOCTYPE html>
<html>
<head>
    <script src="http://code.jquery.com/jquery-1.9.1.js"></script>
    <script type="text/javascript">
$(document).ready(function(){
    $.getJSON('/api/?core=users&function=getJson', function(data) {
  var items = [];
    console.log("ok!");
  $.each(data, function(key, val) {
    items.push('<li id="' + key + '">' + val + '</li>');
  });

  $('<ul/>', {
    'class': 'my-new-list',
    html: items.join('')
  }).appendTo('body');
});
});
</script>
</head>
<body>
<ul class="my-new-list">
<li>test</li>
</ul>
</body>
</html>

JSON datas loaded are :

{"fruit_name":"Tomato"}

Do you have an idea ?

Thank you !

like image 506
Franck Boudraa Avatar asked Oct 20 '25 15:10

Franck Boudraa


1 Answers

If nothing is displayed in the console, then the success function is never getting fired.

Either your server request is returning something other than 200 (like 404, 500, etc) OR the string you're returning is not valid JSON.

Check your network tab to see what is coming back from your request. If it's coming back as 200, then the string you're returning isn't a valid JSON string.

Validate it at: jsonlint.com

like image 66
Adam Avatar answered Oct 22 '25 03:10

Adam