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 apm2.8飞控罗盘bad health,加速度计校准失败
  • ¥15 求解O-S方程的特征值问题给出边界层布拉休斯平行流的中性曲线
  • ¥15 谁有desed数据集呀
  • ¥20 手写数字识别运行c仿真时,程序报错错误代码sim211-100
  • ¥15 关于#hadoop#的问题
  • ¥15 (标签-Python|关键词-socket)
  • ¥15 keil里为什么main.c定义的函数在it.c调用不了
  • ¥50 切换TabTip键盘的输入法
  • ¥15 可否在不同线程中调用封装数据库操作的类