在没有jQuery的情况下使用JSON数据(没有getJSON) [英] Consuming JSON data without jQuery (sans getJSON)

查看:89
本文介绍了在没有jQuery的情况下使用JSON数据(没有getJSON)的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

How can I consume a JSON document without jQuery? Instead of calling the method getJSON(), I'd like to design my own. How do I do that?

推荐答案

如果是相同的域请求,请使用window.XMLHttpRequest。如果它是远程的,然后注入一个脚本元素,你可以看到jQuery是如何做到的:

If it's the same domain request then use window.XMLHttpRequest. If it's remote, then inject a script element, you can see how jQuery does it:

    // 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解析器。

Use a JSON Parser. You can also use eval but it's frowned upon in favor of a JSON parser.

这是jQuery的内部parseJSON方法:

Here's jQuery's internal parseJSON method:

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 );
    }
},

这篇关于在没有jQuery的情况下使用JSON数据(没有getJSON)的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆