0

I have a little script to request json data from my database. Here is the Code

    <script type="text/javascript" src="http://ajax.googleapis.com/ajax/libs/jquery/1.8.0/jquery.min.js"></script>
<script type="text/javascript">
$(function() {
    $(document).ready(function() {
        var keyword = "1";
         var baseurl1 = "json_data2.php?l=" + keyword;
        // Product Count 
        var json = (function() {
            var json = null;
            $.ajax({
                'async': false,
                'global': false,
                'url': baseurl1,
                'dataType': "json",
                'success': function(data) {
                    json = data;
                }
            });
            return json;
        })();
        console.log(json);

    });
});
</script>

When my Feed looks like this

{"limit": [{"rows":"127"}]}

I get "Object {limit: Array[1]}" in the console. So it seems to word. I can also alert json.limit[0].rows and get 127 shown. So far so good.

When I try to get another feed of data which looks like this

{"posts": [{"productname":"Das kleine Ich bin ich","link":"if3OHlvncaIY7A7VGze7VSIeAAZAIZV83cvaG%2B5w3U48cuooMp9qZZJkdQzwyGEXgk8LCR9kD7nY6Y%2FSR0RnjRJo44jHguoaesLAa4mLhuKpuLsfty85ZaePH%2FHReJTc","imgurl":"img\/products\/mytoys\/das-kleine-ich-bin-ich.jpg","price":"13.90","oldprice":"","sale":""},]}

the console log shows up with "null". What could be the issue? I mean I change nothing on the code, just the Url. The datafeed is JSON and I also visited the datafeed in my browser, it's showing up correctly. Any pointers?

4

1 回答 1

2

console.log 记录 null 因为当执行该行时,ajax 执行尚未完成,您的控制台日志以及您想要对 ajax 调用返回的数据执行的任何操作都必须在成功回调中

于 2012-12-06T18:53:46.723 回答