我在网络开发方面有点新手,但我尝试使用Angular CLI创建网站,而且联系页面需要有一个表格来直接发送电子邮件给公司。我已经制作了表格,一切正常。所有路由都已完成并正常工作。但是,每当我尝试对php文件执行http POST时,我都会找到404:
zone.js:2019 POST http://localhost:4200/src/assets/email.php 404(未找到)
我在这里看到了有关此主题的其他问题的TONS,但从未找到任何已解决的问题。我知道URL是正确的,因为当我转到该URL时,php文件将自动下载到我的计算机。很清楚它在那里,但我肯定在某个地方出了问题。谁能帮我吗?这是执行HTTP POST的功能(位于emailform.service.ts中的服务中):
sendEmail(request: EmailRequest): Observable<EmailRequest> | any {
console.log("REQUEST : \n" + "FROM : " + request.email + ",\nNAME : " + request.name +", \n"
+ "MSG : " + request.message +
",\nTO : " + request.toaddr);
return this.http.post(this.emailPathPHP, request)
.map(response => {
console.log('Sending email was successfull', response);
return response;
})
.catch(error => {
console.log('Sending email got error', error);
return Observable.throw(error)
})
}
这是PHP文件(在教程的帮助下制作):
<?php
header('Content-Type: application/json');
$errors='';
if(empty($errors)) {
$postdata = file_get_contents("php://input", true);
$request = json_decode($postdata);
$from_email = $request->email;
$message = $request->message;
$from_name = $request->name;
$to_email = $request->toaddr;
$contact="<p><strong>Name:</strong> $from_name</p>
<p><strong>Email:</strong>$from_email</p>";
$content = "<p>$message</p>"
$website = "advanced-teknoleds";
$email_subject = "$website: Email from $from_name.";
$email_body = '<html><body>';
$email_body .= "$contact $content";
$email_body .= '</body></html>';
$headers .= "MIME-Version: 1.0\r\n";
$headers .= "Content-Type: text/html; charset=ISO-8859-1\r\n";
$headers .= "From: $from_email\n";
$headers .= "Reply-To: $from_email";
mail($to_email, $email_subject, $email_body, $headers);
$response_array['status'] = 'success';
$response_array['from'] = $from_email;
echo json_encode($response_array);
echo json_encode($from_email);
header($response_array);
return $from_email;
}
else {
$response_array['status'] = 'error';
echo json_encode($response_array);
header('Location: /error.html');
}
&GT;
任何帮助将不胜感激!!感谢。