简体   繁体   中英

getJSON doesn't work on the server but does locally

I am using jQuery for a cascading check box, but the getJSON command dosen't work on the server (locally it works fine). It couldn't find the data.json file (see error debug).

Part of the script:

<script type="text/javascript" src="scripts/jquery-1.7.2.js"></script>
<script>
    $(function() {

        $("#json-one").change(function() {

            var $dropdown = $(this);

            $.getJSON("data.json?callback=?", function(data) {

                var key = $dropdown.val();
                var vals = [];

                switch(key) {
                    case 'BR9':
                        vals = data.BR9.split(",");
                        break;
                    case 'base':
                        vals = ['Please choose from above'];
                }

                var $jsontwo = $("#json-two");
                $jsontwo.empty();
                $.each(vals, function(index, value) {
                    $jsontwo.append("<option>" + value + "</option>");
                });

            });
        });

    });
</script>

Error from firebug:

GET http://______my url site ____/data.json 404 NOT FOUND x 25ms

If I change the line

$.getJSON("data.json", function(data) ...

to

$.getJSON("data.json?callback=?", function(data) ...

it doesn't work either.

Can anyone help me?

您是否尝试提供json文件的完整路径?

$.getJSON("http://www.mywebsite.com/folder/data.json?callback=?", function(data) {});

Is the page you are making JSON call from and the page you are making the call to are on the same domain ? JSON doesn't support cross domain calls and you would need to use JSONP.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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