简体   繁体   English

在jquery中成功调用ajax后,为什么不加载此函数?

[英]Why doesn't this function load after a successful ajax call in jquery?

I'm using the tutorial here , that used to work last month, but now it isn't. 我在这里使用的是上个月使用过的教程 ,但现在不是。 I've copied the relevant code below. 我已经在下面复制了相关代码。

$(document).ready(function () {
    $.ajax({
        type: "GET",
        url: "http://papermashup.com/demos/jquery-xml/books.xml",
        dataType: "xml",
        success: xmlParser
    });
    alert("123");
});

function xmlParser(xml) {
    alert("456");

    $('#load').fadeOut();

    $(xml).find("Book").each(function () {

        $(".main").append('<div class="book"><div class="title">' + $(this).find("Title").text() + '</div><div class="description">' + $(this).find("Description").text() + '</div><div class="date">Published ' + $(this).find("Date").text() + '</div></div>');
        $(".book").fadeIn(1000);

    });

}

The problem is that the xmlParser() function isn't being called after the successful ajax request. 问题是成功的ajax请求后没有调用xmlParser()函数。 It shows a 123 alert but not a 456 alert. 它显示123警报,但不显示456警报。 Am I doing something wrong, or is the tutorial wrong? 我做错了什么,还是本教程错了?

I've included a relevant jsfiddle here. 我在这里包括了一个相关的jsfiddle。 http://jsfiddle.net/desbest/nwt3unxu/ http://jsfiddle.net/desbest/nwt3unxu/

Why doesn't this function load after a successful ajax call in jquery? 在jquery中成功调用ajax后,为什么不加载此函数?

It does. 是的

From the JavaScript error console: 从JavaScript错误控制台:

XMLHttpRequest cannot load http://papermashup.com/demos/jquery-xml/books.xml . XMLHttpRequest无法加载http://papermashup.com/demos/jquery-xml/books.xml No ' Access-Control-Allow-Origin ' header is present on the requested resource. 所请求的资源上不存在“ Access-Control-Allow-Origin ”标头。 Origin ' http://fiddle.jshell.net ' is therefore not allowed access. 因此,不允许访问源“ http://fiddle.jshell.net ”。

Your request isn't successful (at least in Ajax terms, it is in strictly HTTP terms, but the site you are requesting the data from isn't giving the browser permission to give the data to the JavaScript from JSFiddle). 您的请求没有成功 (至少以Ajax而言,严格来说是HTTP术语,但是您请求数据的站点没有授予浏览器许可,以将数据从JSFiddle提供给JavaScript)。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM