【发布时间】:2015-02-26 22:25:57
【问题描述】:
我最近刚刚学会了如何在 Wordpress 中使用 Ajax。
我正在尝试在自己的网站上进行练习,但现在遇到了一个问题: 使用 $.post 时我不能使用“beforesend”选项!
在网上搜索时,我只找到了 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,我会在“动作”部分指定函数名称:
$.post(
ajaxurl,
{
'action': 'submin_contact_form',
},
function(response){
alert('The server responded: ' + response);
}
);
谁能给我一个明确的例子/解决方案/例子+这个问题的解决方案?
**注意:这是我第一次移动整个构建的网站并使用 Ajax 和 wordpress,我刚刚学会构建插件 - 我没有很多经验。如果我在这里发布的代码有问题,请善待并提供帮助:)
【问题讨论】: