阿贾克斯成功功能 [英] Ajax success function
问题描述
我使用的是Ajax的职位,表单数据提交到服务器,验证,然后返回基于与否的数据的信息是有效的,可以存储。我在阿贾克斯后我的成功功能不,虽然运行。这里是阿贾克斯后的成功消息的显示:
I am using an Ajax post to submit form data to the server, be validated and then return a message based on whether or not the data was valid and could be stored. My success function in my ajax post doesn't run though. Here is the ajax post and the displaying of the success message:
jQuery.ajax({
type:"post",
dataType:"json",
url: myAjax.ajaxurl,
data: {action: 'submit_data', info: info},
success: function(data) {
successmessage = 'Data was succesfully captured';
}
});
$("label#successmessage").text(successmessage);
$(":input").val('');
return false;
没有得到消息,虽然显示的标签上。我试图在code中的successmessage变量设置为设定值,并将其显示正常,所以一定有什么错我的成功的功能,我看不出有什么?我也尝试设置这样的错误回调:
No message gets displayed on the label though. I tried setting the successmessage variable to a set value in the code and it displayed fine, so there must be something wrong with my success function, I just can't see what? I also tried setting the error callback like this:
error: function(data) {
successmessage = 'Error';
},
但仍然得到不显示消息
But still no message gets displayed
推荐答案
这是因为Ajax是异步的,成功
或错误
功能将在后面叫,当服务器应答客户端。因此,只要将部分取决于结果到您的成功之类的函数是:
It is because Ajax is asynchronous, the success
or the error
function will be called later, when the server answer the client. So, just move parts depending on the result into your success function like that :
jQuery.ajax({
type:"post",
dataType:"json",
url: myAjax.ajaxurl,
data: {action: 'submit_data', info: info},
success: function(data) {
successmessage = 'Data was succesfully captured';
$("label#successmessage").text(successmessage);
},
error: function(data) {
successmessage = 'Error';
$("label#successmessage").text(successmessage);
},
});
$(":input").val('');
return false;
这篇关于阿贾克斯成功功能的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!