WordPress AJAX总是返回404错误

时间:2013-08-19 作者:sublimelaconic

我刚从一个教程中得到了这段代码。我似乎无法让它发挥作用。

HTML(home.php)

    <form name="myform" id="myform" action="" method="POST">  
      <!-- The Name form field -->
      <label for="name" id="name_label">Name</label>  
      <input type="text" name="name" id="name" size="30" value=""/>  
      <br>
      <!-- The Email form field -->
      <label for="email" id="email_label">Email</label>  
      <input type="text" name="email" id="email" size="30" value=""/> 
      <br>
      <!-- The Submit button -->
      <input type="submit" name="submit" value="Submit"> 
   </form>
   <!-- We will output the results from process.php here -->
   <div id="results"><div>
PHP(function.PHP)

    function myform(){
       echo "Form submitted successfully: <br>Your name is <b>".$_POST[\'name\']."</b> and your email is <b>".$_POST[\'email\']."</b><br>"; 
    }
    add_action(\'wp_ajax_myform\', \'myform\');
    add_action(\'wp_ajax_nopriv_myform\', \'myform\');
Javascript(header.php)

    <script type="text/javascript">
jQuery(document).ready(function(){
    jQuery("#myform").validate({
        debug: false,
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            name: "Please let us know who you are.",
            email: "A valid email will help us get in touch with you.",
        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            jQuery.post(\'/wp-admin/admin-ajax.php\', jQuery("#myform").serialize(), function(data) {
                jQuery(\'#results\').html(data);
            });
        }
    });
});
</script>

4 个回复
SO网友:Dream Ideation

这让我很沮丧。我在这个问题上花了几个小时,发现您的问题在于以下输入:

<input type="text" name="name" id="name" size="30" value=""/> 
尝试将输入字段名称更改为除“名称”以外的任何名称,例如:

<input type="text" name="user_name" id="name" size="30" value=""/> 

SO网友:s_ha_dum

如果您在AJAX请求中得到404,那么您的路径是错误的。使用admin_url 构建路径,而不是对其进行硬编码。

jQuery.post(<?php admin_url(\'admin-ajax.php\') ?>, // ...
您最好通过以下方式将管理员url地址打印到Javascript变量wp_localize_script 然后直接在模板中打印。例如:

wp_enqueue_script(\'jquery\');   
wp_localize_script( \'jquery\', \'my_ajax_vars\', array(
    \'ajaxurl\'       => admin_url( \'admin-ajax.php\' )
);
你通常过得更好registeringenqueuing 而不是直接将其写入模板,尤其是header.php. 如前所述,javascript在每个页面上加载的内容,我怀疑每个页面上都需要它。

SO网友:jepser

首先,清理所有数据。

其次,我宁愿这样做:

jQuery(function($){
    $("#myform").validate({
        debug: false,
        rules: {
            name: "required",
            email: {
                required: true,
                email: true
            }
        },
        messages: {
            name: "Please let us know who you are.",
            email: "A valid email will help us get in touch with you.",
        },
        submitHandler: function(form) {
            // do other stuff for a valid form
            //if you don\'t have your script localized and you will add it in header.php (not a good practice)
            $.post(
               \'<?php echo admin_url(\'admin-ajax.php); ?>\',
               form.serialize(),
               function(data){
                   //just to check the data
                   console.log(data);
                   $(\'#results\').html(data);
               }

            });
            jQuery.post(\'/wp-admin/admin-ajax.php\', jQuery("#myform").serialize(), function(data) {
                jQuery(\'#results\').html(data);
            });
        }
    });
});
在php(functions.php)文件中:

确保添加模具();在函数末尾。

function myform(){
       echo "Form submitted successfully: <br>Your name is <b>".$_POST[\'name\']."</b> and your email is <b>".$_POST[\'email\']."</b><br>"; 
       die();
    }
    add_action(\'wp_ajax_myform\', \'myform\');
    add_action(\'wp_ajax_nopriv_myform\', \'myform\');

SO网友:Rob Vermeer

您忘记将(wp)操作放入表单或jQuery ajax函数中。

添加<input type="hidden" name="action" value="myform"> 它将与ajax函数挂钩。

结束