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

查看:92
本文介绍了在jquery中成功调用ajax后,为什么不加载此函数?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用的是此处的教程,该教程上个月曾使用过,但现在不是.我已在下面复制了相关代码.

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

    });

}

问题在于成功的ajax请求后没有调用xmlParser()函数.它显示123警报,但不显示456警报.我是在做错什么,还是本教程是错的?

The problem is that the xmlParser() function isn't being called after the successful ajax request. It shows a 123 alert but not a 456 alert. Am I doing something wrong, or is the tutorial wrong?

我在这里包括了一个相关的jsfiddle. http://jsfiddle.net/desbest/nwt3unxu/

I've included a relevant jsfiddle here. http://jsfiddle.net/desbest/nwt3unxu/

推荐答案

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

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

可以.

从JavaScript错误控制台:

From the JavaScript error console:

XMLHttpRequest无法加载http://papermashup.com/demos/jquery-xml/books.xml.请求的资源上没有'Access-Control-Allow-Origin'头.因此,不允许访问源'http://fiddle.jshell.net'.

XMLHttpRequest cannot load http://papermashup.com/demos/jquery-xml/books.xml. No 'Access-Control-Allow-Origin' header is present on the requested resource. Origin 'http://fiddle.jshell.net' is therefore not allowed access.

您的请求不成功(至少以Ajax术语来说,严格来说是HTTP术语,但是您请求数据的网站并未授予浏览器许可以提供数据到JSFiddle的JavaScript).

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).

这篇关于在jquery中成功调用ajax后,为什么不加载此函数?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

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