[英]"Notice: Undefined index" Error when calling php script from QT Application
[英]Getting 409 error when calling php from my React application
我正在尝试从我的 React 应用程序调用一个简单的 php 文件,该文件将发送一个 email,其中包含联系表中的详细信息。 出于某种原因,当 React 代码执行 PHP 文件的提取时,它返回 409。但是,如果我手动将 URL 发布到另一个选项卡中,它会按预期工作,并且来自我的 React 应用程序的后续调用会按预期工作!
这是我的反应代码:
var url = '/backend/sendmail.php?subject=New Website Enquiry&to=info@site.co.uk&msg=' + msg
console.log(url)
console.log('sending')
fetch(url,
{
'headers': {
'Accept': 'text/html',
'Content-Type': 'text/html'
},
'method': 'GET',
})
.then(
(result) => {
console.log(result.status)
if (result.status === 200) {
console.log('success')
this.togglePop();
this.setState({
name: "",
email: "",
phone: "",
message: "",
terms: false,
})
} else {
console.log('failed')
this.setState({ openError: true })
}
},
(error) => {
console.log('ERROR')
console.log(error)
this.setState({ openError: true })
}
)
这是我的 PHP 文件:
<?php
//header("Access-Control-Allow-Origin: *");
header('Content-Type: text/html');
// error handler function
function customError($errno, $errstr) {
error_log($errstr);
http_response_code(500);
}
// set error handler
set_error_handler("customError");
http_response_code(200);
// send email
mail($_GET["to"],$_GET["subject"],$_GET["msg"],"From: donot.reply@site.co.uk","-f donot.reply@site.co.uk");
error_log($_GET["subject"].":\n".$_GET["msg"], 0);
echo 'OK';
?>
我花了几天时间试图弄清楚为什么会这样。 My.htaccess 文件似乎没问题,因为一旦我成功调用了 PHP 文件,它就可以正常工作了!
这不是 CORS 问题,因为该文件位于同一域中。
有人有什么想法吗?
您向服务器发送了错误的请求,这就是您收到409
错误的原因。 您应该在发送请求之前对 URL 参数进行编码
const url = '/backend/sendmail.php?subject=New Website Enquiry&to=info@site.co.uk&msg=' + msg;
const encoded = encodeURI(url);
console.log(encoded)
// expected correct URI: "/backend/sendmail.php?subject=New%20Website%20Enquiry&to=info@site.co.uk&msg="
你可以在这里阅读更多相关信息
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.