简体   繁体   English

找不到对php的Ajax调用,错误404

[英]Ajax call to php not found, 404 error

I'm trying to build my own WordPress theme and it seems to work but I'm having problems with my email form. 我正在尝试构建自己的WordPress主题,它似乎可以工作,但是我的电子邮件表单存在问题。 The code worked when I used it before adapting to WordPress but now it doesn't. 当我在适应WordPress之前使用它时,该代码有效,但现在不起作用了。

$(function() {
$("#contactForm input,#contactForm textarea").jqBootstrapValidation({
    preventSubmit: true,
    submitError: function($form, event, errors) {
        // additional error messages or events
    },
    submitSuccess: function($form, event) {
        // Prevent spam click and default submit behaviour
        $("#btnSubmit").attr("disabled", true);
        event.preventDefault();
        console.log ('1');

        // get values from FORM
        var name = $("input#name").val();
        var email = $("input#email").val();
        var phone = $("input#phone").val();
        var message = $("textarea#message").val();
        var firstName = name; // For Success/Failure Message

        // Check for white space in name for Success/Fail message
        if (firstName.indexOf(' ') >= 0) {
            firstName = name.split(' ').slice(0, -1).join(' ');
            console.log ('2');
        }

        $.ajax({
            url: "./mail/contact_me.php",
            type: "POST",
            data: {
                name: name,
                phone: phone,
                email: email,
                message: message
            },
            cache: false,
            success: function() {
                // Enable button & show success message
                console.log ('3');

                $("#btnSubmit").attr("disabled", false);
                $('#success').html("<div class='alert alert-success'>");
                $('#success > .alert-success').html("<button type='button' class='close' data-dismiss='alert' aria-hidden='true'>&times;")
                    .append("</button>");
                $('#success > .alert-success')
                    .append("<strong>Your message has been sent. </strong>");
                $('#success > .alert-success')
                    .append('</div>');

The JavaScript file is in this route: domain/wp-content/themes/federo/js/contact_me.js JavaScript文件位于以下路径中: domain/wp-content/themes/federo/js/contact_me.js

And the PHP file is here: domain/wp-content/themes/federo/mail/contact_me.php PHP文件在这里: domain/wp-content/themes/federo/mail/contact_me.php

Even though @ruslan-nigmatulin answer will work, it's better to pass that URL (and any other things you need) wp_localize_script function, like so: 即使@ ruslan-nigmatulin答案有效,也最好传递该URL(以及您需要的其他任何东西) wp_localize_script函数,如下所示:

wp_localize_script( 'ajax-script', 'ajax_object', array( 'ajax_url' => get_template_directory_uri() . '/mail/contact_me.php');

In this example: 在此示例中:
- ajax-script is the handle you've used with wp_enqueue_script wp_enqueue_script ajax-script是您与wp_enqueue_script使用的wp_enqueue_script
- ajax_object is the name of the object that will be available to your script ajax_object是将可用于脚本的对象的名称
- 3rd paramater is an array of values you'd like to pass -第三参数是您要传递的值数组

Then to get your ajax_url in JS, you will simply do ajax_object.ajax_url . 然后,要在JS中获取ajax_url ,只需执行ajax_object.ajax_url In this case (assuming federo is your current theme), ajax_object.ajax_url will be http://domain/wp-content/themes/federo/mail/contact_me.php 在这种情况下(假设federo是您当前的主题), ajax_object.ajax_url将为http://domain/wp-content/themes/federo/mail/contact_me.php

Try to change 尝试改变

url: "./mail/contact_me.php",

into absolute path 进入绝对路径

url: "http://domain/wp-content/themes/federo/mail/contact_me.php",

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM