我最近刚刚学会如何将Ajax与Wordpress结合使用。
我正试着在自己的网站上练习,现在我遇到了一个问题:使用$.post时,我不能使用"beforesend"选项!
在网上搜索时,我只找到了2个关于这个问题的相关帖子:
使用beforeEnd并用$.post完成?
https://wordpress.stackexchange.com/questions/46529/how-to-set-beforsend-option-in-wordpress-ajax-methods/46534#46534
它们可能包含信息,但与我的代码无关:
// 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
,我会在"操作"部分指定函数名称:
$.post(
ajaxurl,
{
'action': 'submin_contact_form',
},
function(response){
alert('The server responded: ' + response);
}
);
有人能给我一个清楚的例子/解决方案/例子+解决方案吗?
**注意:这是我第一次移动一个完整的网站并使用Ajax和wordpress,我刚刚学会了构建插件——我没有太多经验。如果我在这里发布的代码有问题,请提供帮助:)
$.post()
只是$.ajax
的简写方法,$.get()
、load()
和$.getJSON()
也是如此
简写方法已经包含了$.ajax()
中的一些选项,它们只是一个方便的包装器,因此您不必应用许多设置来使用它们。
如果简写方法没有为您提供足够的选项,那么您需要使用低级别的$.ajax
,这将允许您在许多其他选项中设置beforeSend
以及