简体   繁体   English

jQuery .getJSON()URL错误

[英]jQuery .getJSON() URL Error

This is the JSON data that is retrieved when I use the (METHOD REDACTED) method: 这是我使用(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
            }
        }]
    }
}]

Here is my other relevant code: 这是我的其他相关代码:

<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>

I've used .ajax and I've gotten nothing and now I'm trying to use .getJSON . 我使用了.ajax ,却一无所获,现在我正在尝试使用.getJSON I still haven't been able to retrieve the data from the website. 我仍然无法从网站上检索数据。 It seems to never want to get past .getJSON(url, function(data) 似乎永远都不想过去.getJSON(url, function(data)

My Questions 我的问题

1.Why won't my code let me get into my .getJSON function? 1.为什么我的代码不能让我进入.getJSON函数?

2.What do I need to change to access my data in my .getJSON function? 2.我需要进行.getJSON更改才能访问.getJSON函数中的数据?

add the fail function to getJson method and see if you get any errors there 将失败函数添加到getJson方法中,看看是否在其中遇到任何错误

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

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM