为什么这个JavaScript不起作用? - AJAX跨域插件

时间:2013-03-13 02:17:12

标签: javascript jquery ajax

好吧,所以我一直在尝试使用我的API将make代码用于网站。

//Get ID Query String
(function($) {
    $.QueryString = (function(a) {
        if (a == "") return {};
        var b = {};
        for (var i = 0; i < a.length; ++i)
        {
            var p=a[i].split('=');
            if (p.length != 2) continue;
            b[p[0]] = decodeURIComponent(p[1].replace(/\+/g, " "));
        }
        return b;
    })(window.location.search.substr(1).split('&'))
})(jQuery);

$.get('http://api.sidewaykill.com/steam/name.php?id=' + $.QueryString.id)
    .success(function(data) {
    $('#steamname').html(data);
});
$.get('http://api.sidewaykill.com/steam/avatar2.php?id=' + $.QueryString.id)
    .success(function(data) {
    $('#avatar').html(data);
});

显然,由于跨域规则,如果没有一些插件/ modding,这是行不通的。所以我找到了以下代码:

/**
 * jQuery.ajax mid - CROSS DOMAIN AJAX 
 * ---
 * @author James Padolsey (http://james.padolsey.com)
 * @version 0.11
 * @updated 12-JAN-10
 * ---
 * Note: Read the README!
 * ---
 * @info http://james.padolsey.com/javascript/cross-domain-requests-with-jquery/
 */

jQuery.ajax = (function(_ajax){

var protocol = location.protocol,
    hostname = location.hostname,
    exRegex = RegExp(protocol + '//' + hostname),
    YQL = 'http' + (/^https/.test(protocol)?'s':'') + '://query.yahooapis.com/v1/public/yql?callback=?',
    query = 'select * from html where url="{URL}" and xpath="*"';

function isExternal(url) {
    return !exRegex.test(url) && /:\/\//.test(url);
}

return function(o) {

    var url = o.url;

    if ( /get/i.test(o.type) && !/json/i.test(o.dataType) && isExternal(url) ) {

        // Manipulate options so that JSONP-x request is made to YQL

        o.url = YQL;
        o.dataType = 'json';

        o.data = {
            q: query.replace(
                '{URL}',
                url + (o.data ?
                    (/\?/.test(url) ? '&' : '?') + jQuery.param(o.data)
                : '')
            ),
            format: 'xml'
        };

        // Since it's a JSONP request
        // complete === success
        if (!o.success && o.complete) {
            o.success = o.complete;
            delete o.complete;
        }

        o.success = (function(_success){
            return function(data) {

                if (_success) {
                    // Fake XHR callback.
                    _success.call(this, {
                        responseText: (data.results[0] || '')
                            // YQL screws with <script>s
                            // Get rid of them
                            .replace(/<script[^>]+?\/>|<script(.|\s)*?\/script>/gi, '')
                    }, 'success');
                }

            };
        })(o.success);

    }

    return _ajax.apply(this, arguments);

};

})(jQuery.ajax);

不幸的是,正如您在http://sidewaykill.com/apitest.php?id=76561198017389807所看到的,它不起作用。但它在这里工作:api.sidewaykill.com/steam/testing.php?id = 76561198017389807。 Cross Domain AJAX mod有什么问题?或者我做错了什么?

编辑:我确实找到了一个似乎做我想要的小提琴,但我如何改变它以满足我的需要,详见第一个代码部分。 http://jsfiddle.net/schawaska/9vqSP/

0 个答案:

没有答案
相关问题