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

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.

    评论

报告相同问题?

悬赏问题

  • ¥15 怎么获取下面的: glove_word2id.json和 glove_numpy.npy 这两个文件
  • ¥15 js调用html页面需要隐藏某个按钮
  • ¥15 ads仿真结果在圆图上是怎么读数的
  • ¥20 Cotex M3的调试和程序执行方式是什么样的?
  • ¥20 java项目连接sqlserver时报ssl相关错误
  • ¥15 一道python难题3
  • ¥15 牛顿斯科特系数表表示
  • ¥15 arduino 步进电机
  • ¥20 程序进入HardFault_Handler
  • ¥15 oracle集群安装出bug