gpt4 book ai didi

php - 通过 Ajax 向 PHP 发送数据?

转载 作者:行者123 更新时间:2023-12-01 03:08:15 25 4
gpt4 key购买 nike

我需要发送一封电子邮件,当我向mail.php发送请求时,数据值为空。我无法弄清楚为什么会发生这种情况,并且尝试了多种不同的方法。

ajax:

$.ajax({
url: 'mail.php',
action: 'POST',
data: {
'name_first': "First",
'name_last': "Last",
'title': "Test Title",
'topic': "Test Topic",
'mailer': "example@example.com",
'mail': "This is a message"
},
success: function (response) {
alert(response)
},
error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}});

php:

<?php

require_once 'swiftmailer/lib/swift_required.php';

$inf_name = $_POST['name_first'] . ' ' . $_POST['name_last'];
$inf_title = $_POST['title'];
$inf_topic = $_POST['topic'];
$inf_mailer = $_POST['mailer'];
$inf_message = $_POST['mail'];

$transport = Swift_SmtpTransport::newInstance('ssl://smtp.gmail.com', 465)
->setUsername('business@astronstudios.com')
->setPassword('...');

$mailer = Swift_Mailer::newInstance($transport);

$message = Swift_Message::newInstance("[" . $inf_topic . "] " . $inf_title)
->setFrom(array($inf_mailer => $inf_name))
->setTo(array('business@astronstudios.com' => 'AstronStudios'))
->setBody($inf_message, 'text/html');

return $mailer->send($message);

最佳答案

设置type属性而不是action来设置AJAX请求的方法(GETPOST) 。所以代码看起来像这样,

$.ajax({
url: 'mail.php',
type: 'POST',
data: {
'name_first': "First",
'name_last': "Last",
'title': "Test Title",
'topic': "Test Topic",
'mailer': "example@example.com",
'mail': "This is a message"
},
success: function (response) {
alert(response)
},
error: function(xhr, textStatus, error){
console.log(xhr.statusText);
console.log(textStatus);
console.log(error);
}});

引用:http://api.jquery.com/jquery.ajax/

关于php - 通过 Ajax 向 PHP 发送数据?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/40056094/

25 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com