javascript - 在没有 jQuery 的情况下使用 JSON 数据(没有 getJSON)

标签 javascript jquery json

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

最佳答案

如果是相同的域请求,则使用 window.XMLHttpRequest。如果是远程的,那么注入(inject)一个script元素,可以看看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 );
    }
},

关于javascript - 在没有 jQuery 的情况下使用 JSON 数据(没有 getJSON),我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/3238457/

相关文章:

javascript - 如何使用 JavaScript 在 HTML 数据表中可视化 MySQL 表中超过 1000 行的数据?

javascript - 如何使用 javascript 构建动画?

javascript - 当另一个用户更新 mysql 中的表时,我可以在不使用 setInterval() 函数的情况下触发 javascript 函数吗

javascript - jQuery 在悬停时交换图像,然后在悬停时将其替换为原始图像

javascript - 如何使用 JS 在鼠标移动时将 div 的中心定位到鼠标光标的中心?

javascript - 将 javascript 对象键与输入值进行比较

java - 将 JSON 数据从 JavaScript 发送到服务器上的 Java 程序

jquery - 1.7.2 中的顺序 jQuery 动画

mysql - 在 MySQL 的 JSON 数组字段中查找匹配项

json - jq:获取二维数组的维度