代码之家  ›  专栏  ›  技术社区  ›  sms

这个JQuery Ajax GetJSON调用中的错误是什么?

  •  0
  • sms  · 技术社区  · 12 年前

    我的剧本是

    $(document).ready(function () {
    
            $.getJSON("Sample.js", function (data) { var sample = data.one; $("#html").html(sample); })
            });
    

    Sample.js包含

    { one: "one" }
    

    当我运行这个代码时,我只得到一个空白屏幕。如何获得“一”作为输出?

    当我使用$.get运行时,相同的代码会产生{one:“one”}作为输出。

    2 回复  |  直到 12 年前
        1
  •  0
  •   Fouad Fodail    12 年前

    您的json无效。尝试使用:

    { "one": "one" }
     // ^----- this has to be quoted
    

    您可以在此处检查json的有效性: http://jsonlint.com/

        2
  •  0
  •   Sai Avinash    12 年前

    您正在使用getJSON方法。。所以很明显,o/p将是一个JSON对象。。所以为了以字符串的形式出现,。。调用回调函数并将JSON响应转换为String格式

    您的JSON应该是这样的:

     {  "one": "Singular sensation",  "two": "Beady little eyes",  "three": "Little birds pitch by my doorstep" }