Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/76.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Jquery.getJSON无法处理域之间的跨浏览器,尽管它应该是_Jquery_Ajax_Getjson - Fatal编程技术网

Jquery.getJSON无法处理域之间的跨浏览器,尽管它应该是

Jquery.getJSON无法处理域之间的跨浏览器,尽管它应该是,jquery,ajax,getjson,Jquery,Ajax,Getjson,我正在尝试创建一个jquery小部件,它从服务器加载一些数据,并将其放在客户端浏览器的容器中。因此,不同领域的跨浏览器调用问题需要得到重视。为此,我使用了$.getJSON。下面显示了我的javascript代码: (function() { // Localize jQuery variable var jQuery; /** ****** Load jQuery if not present ******** */ if (window.jQuery === undefined || w

我正在尝试创建一个jquery小部件,它从服务器加载一些数据,并将其放在客户端浏览器的容器中。因此,不同领域的跨浏览器调用问题需要得到重视。为此,我使用了$.getJSON。下面显示了我的javascript代码:

(function() {

// Localize jQuery variable
var jQuery;

/** ****** Load jQuery if not present ******** */
if (window.jQuery === undefined || window.jQuery.fn.jquery !== '1.4.2') {
    var script_tag = document.createElement('script');
    script_tag.setAttribute("type", "text/javascript");
    script_tag
            .setAttribute("src",
                    "http://ajax.googleapis.com/ajax/libs/jquery/1.4.2/jquery.min.js");
    if (script_tag.readyState) {
        script_tag.onreadystatechange = function() { // For old versions
                                                        // of IE
            if (this.readyState == 'complete'
                    || this.readyState == 'loaded') {
                scriptLoadHandler();
            }
        };
    } else { // Other browsers
        script_tag.onload = scriptLoadHandler;
    }
    // Try to find the head, otherwise default to the documentElement
    (document.getElementsByTagName("head")[0] || document.documentElement)
            .appendChild(script_tag);
} else {
    // The jQuery version on the window is the one we want to use
    jQuery = window.jQuery;
    main();
}

/** ****** Called once jQuery has loaded ***** */
function scriptLoadHandler() {
    // Restore $ and window.jQuery to their previous values and store the
    // new jQuery in our local jQuery variable
    jQuery = window.jQuery.noConflict(true);
    // Call our main function
    main();
}

/** ****** Our main function ******* */
function main() {
    jQuery(document)
            .ready(
                    function($) {
                        getTodaysAd();
                        function getTodaysAd() {
                            var url = "http://localhost:8080/TJ/ads/adsLoader";
                            $
                                    .getJSON(
                                            url,
                                            function(data) {
                                                console.log(data);
                                                $("#todaysAd")
                                                        .append(
                                                                "<a id=\"adsDestination\"><img id=\"adsImg\"></a></img>");
                                                $("#adsImg").attr({
                                                    "href" : data[0].href,
                                                    "src" : data[0].imgurl
                                                });
                                                $("#adsDestination").attr({
                                                    "href" : data[0].href
                                                });
                                            });
                        }

                    });
    }

  })(function() {
  }); // We call our anonymous function immediately
但是当我搜索$.getJSON时,它应该能够处理这个问题。有人能帮忙吗

******************更新***************************


当我做同样的事情,但在同一个域中,一切都正常运行时,
$.getJSON
实际上可以跨域运行,您必须确保您从中获取的域已正确设置为允许请求

如错误所述,这可以通过在您试图从中获取的域上放置一个
访问控制允许来源
头来实现


有关添加此标题的更多信息,请查看以下答案:

对于使用apache tomcat的用户,此链接对于设置web.xml非常有用。谢谢您的接受,非常感谢您提供了一些附加信息。
<script src="http://localhost:8080/TJ/js/embed.js" type="text/javascript"></script>
<div id="todaysAd" class="panel-heading"></div>
XMLHttpRequest cannot load http://localhost:8080/TJ/ads/adsLoader. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://localhost:3003' is therefore not allowed access.