【问题标题】:405 error with Ajax POST / Nginx configurationAjax POST / Nginx 配置出现 405 错误
【发布时间】:2017-11-20 14:55:03
【问题描述】:

我正在尝试使用 Ajax 表单和 swiftmailer 发送电子邮件。它在本地工作,但不能在生产环境中工作。

当 contact_me.php 不是从表单获取参数而是明确写入时,电子邮件甚至从服务器发送,所以我认为 Swiftmailer 正在工作。

contact_me.js

// Contact Form Scripts

$(function() {

    $("#contactForm input,#contactForm textarea").jqBootstrapValidation({
        preventSubmit: true,
        submitError: function($form, event, errors) {
            // additional error messages or events
        },
        submitSuccess: function($form, event) {
            event.preventDefault(); // prevent default submit behaviour
            // 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(' ');
            }
            $.ajax({
                url: "././mail/contact_me.php",
                type: "POST",
                data: {
                    name: name,
                    phone: phone,
                    email: email,
                    message: message
                },
                dataType: "text",
                cache: false,
                success: function() {
                    // Success message       
                },
                error: function() {
                    // Fail message                    
                },
            });
        },
        filter: function() {
            return $(this).is(":visible");
        },
    });

    $("a[data-toggle=\"tab\"]").click(function(e) {
        e.preventDefault();
        $(this).tab("show");
    });
});


/*When clicking on Full hide fail/success boxes */
$('#name').focus(function() {
    $('#success').html('');
});

contact_me.php

<?php
// Autoload for swiftmailer
require_once '../vendor/autoload.php';

// Check for empty fields
if(empty($_POST['name'])      ||
   empty($_POST['email'])     ||
   empty($_POST['phone'])     ||
   empty($_POST['message'])   ||
   !filter_var($_POST['email'],FILTER_VALIDATE_EMAIL))
   {
   echo "No arguments Provided!";
   return false;
   }


$name = strip_tags(htmlspecialchars($_POST['name']));
$email_address = strip_tags(htmlspecialchars($_POST['email']));
$phone = strip_tags(htmlspecialchars($_POST['phone']));
$message = strip_tags(htmlspecialchars($_POST['message']));

// Create the email and send the message
$email_subject = "TrustPair nouveau contact :  $name";
$email_body = "New form.\n\n"."Here are the details:\n\nName: $name\n\nEmail: $email_address\n\nPhone: $phone\n\nMessage:\n$message";

// Add here swiftmailer code, need to return true
// Create the Transport
$transport = (new Swift_SmtpTransport('mail.gandi.net', 465, "ssl"))
  ->setUsername('name@domain.com')
  ->setPassword('password')
;

// Create the Mailer using your created Transport
$mailer = new Swift_Mailer($transport);

// Create the message
$message = (new Swift_Message())
    // Give the message a subject
    ->setSubject($email_subject)
    // Set the From address with an associative array
    ->setFrom(['noreply@domain.com' => 'Domain no reply'])
    // Set the To addresses
    ->setTo(['firstmailto@gmail.com', 'secondmailto@gmail.com'])
    // Give it a body
    ->setBody($email_body)
    ;

// Send the message
$result = $mailer->send($message);
echo $result;
// result is equal to the nbr of message recipients

if ($result == 0) {
    return false;
} else {
    return true;
}


?>

【问题讨论】:

  • 有什么错误吗?
  • jquery.min.js:4 POST example.com/mail/contact_me_test.php405 (不允许) 发送@jquery.min.js:4 ajax @jquery.min.js:4 submitSuccess @contact_me.js:22 (匿名)@jqBootstrapValidation.js:76 调度@jquery.min.js:3 r.handle@jquery.min.js:3
  • 您的代码是url: "././mail/contact_me.php",错误显示:/mail/contact_me_test.php。它甚至没有尝试发布到同一个文件? (并且您还应该删除 URL 前面的 ././mail/...,并从根目录使用相对:/mail/...
  • 我将contact_me.php 替换为一个新文件contact_me_test.php,在该文件中我不使用来自表单的输入,而是使用代码中的值。当我运行“php contact_me_test.php”时,会发送电子邮件。我将路径更改为相对路径,我总是遇到相同的 405 错误。
  • 我不知道它是否有帮助,但是当我在本地使用 wamp 虚拟主机时代码可以工作。但是,如果我使用 browserSync (localhost),我有一个 404 错误 POST localhost:3000/mail/contact_me.php 404 (Not Found),我没有在 prod 中运行 browserSync,所以我不确定它有什么帮助。

标签: php jquery ajax swiftmailer


【解决方案1】:

Nginx 服务器不允许带有静态页面(即 *.html)的 POST 请求。

hacks处理问题。就我而言,它修复了 405 错误,但未发送电子邮件。

解决方案是将 index.html 更改为 index.php,请务必adapt your Nginx configuration 以反映此更改。

【讨论】:

    猜你喜欢
    • 2019-01-24
    • 1970-01-01
    • 2011-12-10
    • 2017-09-09
    • 1970-01-01
    • 1970-01-01
    • 2023-03-25
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多