我刚刚学会了如何在Wordpress中使用Ajax。
我试图在我自己的网站上练习它,现在我遇到了一个问题: 我不能使用" beforesend"使用$ .post时的选项!
在网上搜索时,我发现只有2篇关于这个问题的相关帖子:
using beforeSend and complete with $.post?
它们可能包含但与我的代码无关的信息:
// Submit button for a "contact form".
$('#sendBtn').click(function(){
//get info
var fullname = $("#fullname").val();
var email = $("#email").val();
var text = $("#text").val();
//send info to php
$.ajax({
beforeSend: function() {
if ( IsEmail(email) == false) {
$('#aboutUnsuccess').show("slow");
$('.form_content').hide("slow");
}
},
url: document.location.protocol+'//'+document.location.host+'/wp_admin/admin_ajax.php',
type: "POST",
data: ({ "fullname": fullname, "email": email, "text": text }),
success: function (results){
if ( IsEmail(email) == true) {
//hide table
$('.form_content').hide('slow', function() {
$('.form_content').hide( "slow" );
});
//show textboxes
$('#aboutSuccess').show("slow");
$("#aboutSuccess").append( "<iframe id=\"pixel-thing\" src=\"http://54.149.xx.xx/wp-content/themes/twentyfifteen-child/thePixel.html\" width=\"1\" height=\"1\" border=\"0\"></iframe>" );
}
}
});
});
我在functions.php中有这个部分:
add_action('wp_ajax_nopriv_submin_contact_form', 'submit_contact_form');
function submit_contact_form(){
}
如果我使用$.post
,我会在&#34; action&#34;中指定函数名称。部分:
$.post(
ajaxurl,
{
'action': 'submin_contact_form',
},
function(response){
alert('The server responded: ' + response);
}
);
任何人都可以为此问题提供明确的示例/解决方案/示例+解决方案吗?
**注意:这是我第一次移动整个构建的网站并使用带有wordpress的Ajax,我刚刚学会了构建插件 - 我没有很多经验。如果我在这里发布我的代码有问题,请善待并帮助:)
答案 0 :(得分:1)
$.post()
只是$.ajax
的简写方法,$.get()
,load()
和$.getJSON()
速记方法已经包含了$.ajax()
中的一些选项,而且只是便利包装,因此您不必应用许多设置来使用它们。
如果简写方法没有为您提供足够的选项,那么您需要使用低级别$.ajax
,这样您就可以在其他许多选项中设置beforeSend