【问题标题】:How to set beforeSend option in wordpress ajax methods如何在 wordpress ajax 方法中设置 beforeSend 选项
【发布时间】:2015-02-26 22:25:57
【问题描述】:

我最近刚刚学会了如何在 Wordpress 中使用 Ajax。

我正在尝试在自己的网站上进行练习,但现在遇到了一个问题: 使用 $.post 时我不能使用“beforesend”选项!

在网上搜索时,我只找到了 2 篇关于此问题的相关帖子:

using beforeSend and complete with $.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,我刚刚学会构建插件 - 我没有很多经验。如果我在这里发布的代码有问题,请善待并提供帮助:)

【问题讨论】:

    标签: php jquery ajax wordpress


    【解决方案1】:

    $.post() 只是$.ajax 的简写方法,$.get()load()$.getJSON() 也是如此

    速记方法已经包含 $.ajax() 中的一些选项,并且只是方便的包装器,因此您无需应用许多设置即可使用它们。

    如果速记方法没有为您提供足够的选项,那么您需要使用低级别的$.ajax,这将允许您在许多其他选项中设置beforeSend

    【讨论】:

    • 哦,所以我可以像这样使用它:url: document.location.protocol+'//'+document.location.host+'/wp_admin/admin_ajax.php', type: "POST", data: ({ "fullname": fullname, "email": email, "text": text }), action: my_fucntion ?
    • 我不久前查看了这个链接,并没有发现任何关于“action:”的内容...
    • 您可以将action 添加到您的数据对象中。 data: {action:'submin_contact_form', "fullname": fullname...
    • 哦,那么我之前写的 4 cmets 是错误的做法吗?
    猜你喜欢
    • 1970-01-01
    • 2013-12-25
    • 2012-06-08
    • 2014-08-01
    • 2017-04-09
    • 1970-01-01
    • 2013-04-09
    • 1970-01-01
    • 2015-10-02
    相关资源
    最近更新 更多