weixin_33725515 2015-08-11 10:01 采纳率: 0%
浏览 59

无法加载JSON文件

Following is the json & js file code, unable to run the json file with jquery ajax, always showing error msg.

Running code on the loacl machine without any asp.net / php coding, I want to run JSON without the server. I have set the url through IIS on my local machine.

JSON:

//myTutorials.json file
{
  "one": "Singular sensation",
  "two": "Beady little eyes",
  "three": "Little birds pitch by my doorstep"
}

Javascript:

//main.js file
$(document).ready(function(){       
    $.ajax({
      dataType: "json",
      url: "myTutorials.json",
      format: "json",
      contentType: 'application/json; charset=utf-8',
      success: function(response){      
        alert(response.one);
        alert('success');
      },
      error:function(){
        alert('error');
      }
    });
}); 

HTML:

<!DOCTYPE HTML>
<html>
<head>
<meta charset="UTF-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0"/>
<title>HTML5 responsive website tutorial</title>
<link href='http://fonts.googleapis.com/css?family=Open+Sans|Baumans' rel='stylesheet' type='text/css'/>
<link href='style.css' rel='stylesheet' type='text/css'/>
<script>
</script>
<style>
</style>

<!-- my files -->
<script src="http://code.jquery.com/jquery-1.7.1.min.js"></script>
<script src="main.js"></script>
</head>
    <body>
        <div id="clickHeading"></div>
        <div id="id01"></div>
    </body>
</html>
  • 写回答

1条回答 默认 最新

  • hurriedly% 2015-08-11 12:36
    关注

    As Jozef pointed out, if you don't have a web server you can't request the file with ajax-function. However you can just use the json as a variable and test it out if that is enough for your need:

    $(document).ready(function(){       
        var myJson = {
          "one": "Singular sensation",
          "two": "Beady little eyes",
          "three": "Little birds pitch by my doorstep"
        };  
    
        alert(myJson.one);
    });
    
    评论

报告相同问题?

悬赏问题

  • ¥15 宇视监控服务器无法登录
  • ¥15 PADS Logic 原理图
  • ¥15 PADS Logic 图标
  • ¥15 电脑和power bi环境都是英文如何将日期层次结构转换成英文
  • ¥20 气象站点数据求取中~
  • ¥15 如何获取APP内弹出的网址链接
  • ¥15 wifi 图标不见了 不知道怎么办 上不了网 变成小地球了
  • ¥50 STM32单片机传感器读取错误
  • ¥50 power BI 从Mysql服务器导入数据,但连接进去后显示表无数据
  • ¥15 (关键词-阻抗匹配,HFSS,RFID标签天线)