1

I am trying to parse some JSON data through AJAX i followed an example from here:

how to parse json data with jquery / javascript?

On the example, they can get it working, but on my exmaple, the

turned blank.

I tried just echoing the php, the JSON displayed with no problem either. Wondering what the problem is.

<!DOCTYPE HTML>
<html>
<head>
  <link type ="text/css" rel="stylesheet" href= "css/bootstrap.css">
 <link type ="text/css" rel="stylesheet" href= "css/account.css">
</head>



 <body>
    <p id="result">fefe</p>>
     <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.11.1/jquery.min.js"></script>

<script src="js/bootstrap.min.js"></script>
     <script>
    $.ajax({ 
                    type: 'GET', 
                    url: 'get.php', 
                    data: { get_param: 'value' }, 
                    dataType:'json',
                    success: function (data) { 
                                        var names = data
                        $('#result').html(data);
                    }
                });
        </script>
    </body>
    </html>

What the JSON result looks like in php:

    [{"id":"1","userid":"26","title":"654","description":"654"}]
Community
  • 1
  • 1
Benyaman
  • 451
  • 2
  • 10
  • 25

4 Answers4

1

what is the type of data? try add this line in your code on success function

success: function (data) {
    console.log(data);
}

is it an array of objects, if yes maybe you can try this

$.ajax({ 
    type: 'GET', 
    url: 'get.php', 
    data: { get_param: 'value' }, 
    dataType:'json',
    success: function (data) { 
        for(var i=0; i<data.length; i++) {
            // do your things here using data[i].description until data[i].userid
        }
    }
});
Kyojimaru
  • 2,694
  • 1
  • 14
  • 22
0

Try this

$.ajax({
      type: "GET",
      dataType: "json",
      url: "getjobs.php",
      data: data,
      success: function(data) {
       $('#result').html(data);

        }
      });
sush
  • 379
  • 1
  • 3
  • 11
0

Try setting $.ajax's datatype is set to jsonp. Also try to alert the return value.

      $.ajax({ 
            type: 'GET', 
            url: 'get.php', 
            data: { get_param: 'value' }, 
            dataType:'jsonp',
            success: function (data) { 
                alert('data is ::' +data);
                $('#result').html(data);
            }
        });
ngrashia
  • 9,869
  • 5
  • 43
  • 58
0

Try this:

$.ajax({ 
            type: 'GET', 
            url: 'get.php', 
            data: { get_param: 'value' }, 
            dataType:'json',
            success: function (data) { 
                 console.log(data);
            }
        });

in the console(such as chrome browser`s development tools), you can see the actual result.

mylxsw
  • 86
  • 2
  • 4
  • Everything just displays Object i added a screenshot in the question above – Benyaman Jul 04 '14 at 08:25
  • You should not pass the json object to ```$('#result').html()```, ```$('#result').html()``` only accept string value or callback function as parameter. – mylxsw Jul 04 '14 at 08:34