jQuery .getJSON()URL错误

时间:2014-04-20 03:16:15

标签: javascript jquery json getjson

这是我使用(METHOD REDACTED)方法时检索到的JSON数据:

[{
    "group_option": {
        "OptionsID": "28",
        "MenuGroupID": "6",
        "group_options_name": "Select two (2) sides :",
        "menu_group_option_information": null,
        "menu_group_option_min_selected": "0",
        "menu_group_option_max_selected": "2",
        "fDateAdded": "2014-01-25 08:29:20",

        "group_option_items": [{
            "item": {
                "OptionItemID": "69",
                "menu_item_option_name": "Mexican rice",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "70",
                "menu_item_option_name": "Refried beans",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "71",
                "menu_item_option_name": "Poblano rice",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "72",
                "menu_item_option_name": "Charro beans",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "73",
                "menu_item_option_name": "Caesar salad",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "74",
                "menu_item_option_name": "Southwestern mashed potatoes",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "75",
                "menu_item_option_name": "Roasted corn succotash",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "76",
                "menu_item_option_name": "Poblano cheese corn grits",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "77",
                "menu_item_option_name": "French Fries",
                "menu_item_option_additional_cost": null
            }
        }, {
            "item": {
                "OptionItemID": "78",
                "menu_item_option_name": "Fiesta salad",
                "menu_item_option_additional_cost": null
            }
        }]
    }
}]

这是我的其他相关代码:

<html>

<head>
    <meta charset="utf-8" />
    <script src="../JQUERY/jquery-1.11.0.js"></script>
    <script>
        function getOptions() {
            var html = new String();

            $.getJSON('<URL HERE>', function (data) {
                alert("function");
                //var data = JSON.parse(data);
                var h = new String();
                for (var i = 0; i < data.length; i++) {
                    h += '<div class="data">';

                    h += data[i]['group_option'].OptionsID + '<br>';
                    h += data[i]['group_option'].MenuGroupID + '<br>';
                    h += data[i]['group_option'].group_options_name + '<br>';
                    h += data[i]['group_option'].menu_group_option_information + '<br>';
                    h += data[i]['group_option'].menu_group_option_min_selected + '<br>';
                    h += data[i]['group_option'].menu_group_option_max_selected + '<br>';
                    h += data[i]['group_option'].fDateAdded + '<br><br><br>';

                    for (var iter = 0; iter < data[i]['group_option']['group_option_items'].length; iter++) {

                        h += data[i]['group_option']['group_option_items'][iter]['item'].OptionItemID + '<br>';
                        h += data[i]['group_option']['group_option_items'][iter]['item'].menu_item_option_name + '<br>';
                        h += data[i]['group_option']['group_option_items'][iter]['item'].menu_item_option_additional_cost + '<br>';
                        h += '<br><br><br>';
                    }
                    h += '</div>';
                }
                alert("h");
                alert(h);
                html = h;
                alert("html equals ");
                alert(html);

            });
            alert("returning html");
            alert(html);
            return html;
        }
    </script>
    <script>
        $(document).ready(function () {
            var str = "";
            str = getOptions();
            $('#content').append(str);
        });
    </script>
</head>

<body>
    <div id="content"></div>
</body>

我使用了.ajax而我什么都没有,现在我正在尝试使用.getJSON。我仍然无法从网站上检索数据。似乎永远不想超越.getJSON(url, function(data)

我的问题

1.为什么我的代码不能让我进入我的.getJSON功能?

2.我需要更改以在.getJSON功能中访问我的数据吗?

1 个答案:

答案 0 :(得分:0)

将失败函数添加到getJson方法并查看是否有任何错误

$.getJSON("", function(){
    ....
})
.fail(function( jqxhr, textStatus, error ) {
    var err = textStatus + ", " + error;
    console.log( "Request Failed: " + err );
})