weixin_33686714 2017-12-21 16:12 采纳率: 0%
浏览 40

jQuery getJSON()方法

I have a JSON file in folder App_Data . Now I want to read this file and keep it in a JS variable. This is the code I wrote but it's not working.

$(document).ready(function () {
  $.getJSON("~/App_Data/smartParkTotalJson.json", function(json) {
  });
});

UPDATE

For now, I just want to check if its read. so I wrote this code:

        <script>
        $(document).ready(function () {
            $.getJSON("/App_Data/smartParkTotalJson.json", function (json) {
                alert("sff");
            });
        });
    </script>

I want to get some alert

  • 写回答

1条回答 默认 最新

  • weixin_33691817 2017-12-21 16:15
    关注

    The ~ character is only valid within ASP.Net routing constructs. JS will not translate it to a valid URL. To fix this you need to either use a relative path from the root of the site:

    $.getJSON("/App_Data/smartParkTotalJson.json", function(json) {
    

    Or interpolate Razor in to your code - assuming this JS code is inside an MVC View:

    $.getJSON('@Url.Context("~/App_Data/smartParkTotalJson.json")', function (json) {
    

    Also note that the App_Data folder is configured by default to not respond to HTTP requests. It is intended to hold app-specific information. I'd suggest creating your own folder to host this file.

    评论

报告相同问题?