[英]help with jquery ajax success event
I'm having an issue with my update button and jquery ajax. 我的更新按钮和jquery ajax出现问题。 Right now when I click on my update button, it saves whatever updated data to the database. 现在,当我单击更新按钮时,它将任何更新的数据保存到数据库中。 My goal is I want to slide up a message if the update is successful. 我的目标是,如果更新成功,我想向上滑动一条消息。 I was looking at ajax post and using the success event seems like it would work but I dont know how to incorporte it. 我正在查看ajax帖子,并使用成功事件似乎可以正常工作,但我不知道该如何配合。 How would I do this? 我该怎么做? Would it be something like this? 会是这样吗?
$(document).ready(function(){
$('#divSuccess').hide();
$('#btnUpdate').click( function() {
alert('button click');
$.ajax({
url: "test.aspx",
context: document.body,
success: function(){
$('#divSuccess').show("slide", { direction: "down" }, 3000);
$('#divSuccess').hide("slide", { direction: "down"}, 5000);
}
});
});
});
$("#targetDiv").load("page.php",$("#form").serializeArray(),function (response)
{
if (response == '0' && response != '')
alert('Request not sent to server !\n');
else if(response == '-1')
alert('Please write some more !\n');
else
{
alert("success! ");
}
}
);
i've echo ed 0 and -1 for failure and other for success 我已经为错误0和-1回声,为成功而其他
In the jquery post function, you can execute some callback function. 在jquery post函数中,您可以执行一些回调函数。
function (data, textStatus) {
// data could be xmlDoc, jsonObj, html, text, etc...
this; // the options for this ajax request
// textStatus can be one of:
// "timeout"
// "error"
// "notmodified"
// "success"
// "parsererror"
// NOTE: Apparently, only "success" is returned when you make
// an Ajax call in this way. Other errors silently fail.
// See above note about using $.ajax.
}
With at least jQuery 1.5, you've got deferred objects and new syntax for AJAX events (including success
). 至少使用jQuery 1.5,您已经有了AJAX事件的延迟对象和新语法(包括success
)。
var $ajaxcall = $.ajax({
url : 'myurl.svc/somemethod',
data : '{ somedata : "sometext" }'
});
$ajaxcall.success(function() {
// do something on successful AJAX completion
});
Of course you can chain that as well, and call something along the lines of $.ajax().success()
or something. 当然,您也可以将其链接起来,然后按照$.ajax().success()
调用某些内容。
Just wrote a blog post on it myself , if you're interested in reading more. 如果您有兴趣阅读更多内容,请自己写一篇博客文章 。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.