jQuery 成功后的ajax调用函数
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/6184759/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
Ajax call function after success
提问by curly_brackets
I'm working on a site, where we get information from an XML-file. It work great, but now I need to make a slider of the content. To do this, I will use jCarousel that claims they can do it with Dynamicly loaded content, by calling a callback function.
我在一个网站上工作,我们从一个 XML 文件中获取信息。它工作得很好,但现在我需要制作一个内容滑块。为此,我将使用 jCarousel,声称他们可以通过调用回调函数对动态加载的内容执行此操作。
I can't, however, make the initial ajax load, when I call a function on success. What am I doing wrong?
但是,当我成功调用函数时,我无法进行初始 ajax 加载。我究竟做错了什么?
$(document).ready(function() {
$.ajax({
type: "GET",
//Url to the XML-file
url: "data_flash_0303.xml",
dataType: "xml",
success: hulabula()
});
function hulabula(xml) {
$(xml).find('top').each(function() {
var headline = $(this).find('headline1').text();
var headlineTag = $(this).find('headline2').text();
$(".wunMobile h2 strong").text(headline + " ");
$(".wunMobile h2 span").text(headlineTag);
});
..............
Am I doing anything wrong??? Or is it a totally diffenerent place I have to look? :-)
我做错什么了吗???或者它是一个完全不同的地方,我必须去看看?:-)
回答by istvan.halmen
Use hulabula instead of hulabula() or pass the function directly to the ajax options:
使用 hulabula 而不是 hulabula() 或将函数直接传递给 ajax 选项:
1.
1.
$.ajax({
type: "GET",
//Url to the XML-file
url: "data_flash_0303.xml",
dataType: "xml",
success: hulabula
});
2.
2.
$.ajax({
type: "GET",
//Url to the XML-file
url: "data_flash_0303.xml",
dataType: "xml",
success: function(xml) { /* ... */ }
});
回答by dhinesh
Use $.when
使用$.when
function hulabula(xml) {
$(xml).find('top').each(function() {
var headline = $(this).find('headline1').text();
var headlineTag = $(this).find('headline2').text();
$(".wunMobile h2 strong").text(headline + " ");
$(".wunMobile h2 span").text(headlineTag);
});
}
var ajaxCall = $.ajax({
type: "GET",
//Url to the XML-file
url: "data_flash_0303.xml",
dataType: "xml"
});
//Use deferred objects
$.when(ajaxCall)
.then(function(xml) { hulabula(xml); });
回答by Balsa
You can do it like this:
你可以这样做:
$.post( "user/get-ups-rates", { cart_id: cart_id, product_id: product_id, service_id:service_id })
.done(function( data ) {
el.parent().find('.upsResult').html('UPS Shipping Rate Is $'+data.rate);
})
.fail(function(data) {
el.parent().find('.upsResult').html('<p style="color:red">UPS Service Error Occured. </p>');
})
.complete(function (data) {
setShippingOptions(cart_id,product_id,service_id,data.rate);
});
Just call the function in 'complete' not in 'done' or 'success'
只需在“完成”而不是“完成”或“成功”中调用该函数