【问题标题】:Consuming JSON data without jQuery (sans getJSON)在没有 jQuery 的情况下使用 JSON 数据(无 getJSON)
【发布时间】:2010-07-13 14:58:48
【问题描述】:

如何在没有 jQuery 的情况下使用 JSON 文档?我不想调用getJSON() 方法,而是自己设计。我该怎么做?

【问题讨论】:

  • 嗨维维克。您能否为您的问题添加更多细节?您使用什么语言/框架?
  • +1 显然 OP 想知道如何在不使用 jQuery 的情况下对 JSON 数据进行 AJAX 请求。并不难弄清楚。
  • @patrick:好吧,仅仅用 9 个词就可以引发 cmets 讨论的人已经够多的了,所以我会说这是一个不言自明的问题,不管任何人的能力如何是搞清楚。
  • @annakata - 如果您不知道 getJSON() 和 jQuery 是什么,我想这可能会令人困惑。考虑到原始标签可能就是这种情况。但是这里任何熟悉他们的人(如 David Hedlund)都应该能够很容易地确定所询问的内容。 OP 显然不明白该方法是 jQuery 独有的。这就是为什么 SO 在这里。帮助不懂的人。
  • @David Hedlund - 恕我直言,如果您知道 jQuery 和 getJSON() 是什么,那么找出问题应该很简单。显然,OP不明白一些事情。也许您可以留下有用的评论或答案,而不是讽刺地回复。

标签: javascript jquery json


【解决方案1】:

如果是同一个域请求,则使用 window.XMLHttpRequest。如果是远程的,那么注入一个脚本元素,可以看到jQuery是怎么做的:

    // If we're requesting a remote document
    // and trying to load JSON or Script with a GET
    if ( s.dataType === "script" && type === "GET" && remote ) {
        var head = document.getElementsByTagName("head")[0] || document.documentElement;
        var script = document.createElement("script");
        script.src = s.url;
        if ( s.scriptCharset ) {
            script.charset = s.scriptCharset;
        }

        // Handle Script loading
        if ( !jsonp ) {
            var done = false;

            // Attach handlers for all browsers
            script.onload = script.onreadystatechange = function() {
                if ( !done && (!this.readyState ||
                        this.readyState === "loaded" || this.readyState === "complete") ) {
                    done = true;
                    success();
                    complete();

                    // Handle memory leak in IE
                    script.onload = script.onreadystatechange = null;
                    if ( head && script.parentNode ) {
                        head.removeChild( script );
                    }
                }
            };
        }

        // Use insertBefore instead of appendChild  to circumvent an IE6 bug.
        // This arises when a base node is used (#2709 and #4378).
        head.insertBefore( script, head.firstChild );

        // We handle everything using the script element injection
        return undefined;
    }

使用JSON Parser。您也可以使用eval,但它不赞成使用 JSON 解析器。

这是 jQuery 的内部 parseJSON 方法:

parseJSON: function( data ) {
    if ( typeof data !== "string" || !data ) {
        return null;
    }

    // Make sure leading/trailing whitespace is removed (IE can't handle it)
    data = jQuery.trim( data );

    // Make sure the incoming data is actual JSON
    // Logic borrowed from http://json.org/json2.js
    if ( /^[\],:{}\s]*$/.test(data.replace(/\\(?:["\\\/bfnrt]|u[0-9a-fA-F]{4})/g, "@")
        .replace(/"[^"\\\n\r]*"|true|false|null|-?\d+(?:\.\d*)?(?:[eE][+\-]?\d+)?/g, "]")
        .replace(/(?:^|:|,)(?:\s*\[)+/g, "")) ) {

        // Try to use the native JSON parser first
        return window.JSON && window.JSON.parse ?
            window.JSON.parse( data ) :
            (new Function("return " + data))();

    } else {
        jQuery.error( "Invalid JSON: " + data );
    }
},

【讨论】:

    【解决方案2】:

    您必须推出自己的 JSON/AJAX 函数。有一些例子here。我不确定它们有多好。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 2011-02-11
      • 2017-06-20
      • 2014-10-07
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-04-11
      • 2013-04-01
      相关资源
      最近更新 更多