【问题标题】:Symfony ApiPlatform React - CORSSymfony ApiPlatform React - CORS
【发布时间】:2020-06-15 18:36:57
【问题描述】:

我有一个问题... 使用 APIPlatform 和 React,我想使用 Symfony 邮件程序发送邮件,我使用自定义操作。

这里是我所有的页面来实现我的选择

    <?php
namespace App\Controller;

use App\Entity\Contact;
use Symfony\Bridge\Twig\Mime\TemplatedEmail;
use Symfony\Component\HttpFoundation\Request;
use Symfony\Component\Mailer\MailerInterface;

class ContactMailController {

    private $mail;

    public function __construct(MailerInterface $mailer)
    {
        $this->mailer = $mailer;
    }


    public function __invoke(Contact $data){
        $prenom=htmlspecialchars(strip_tags($data->getPrenom()));
        $nom=htmlspecialchars(strip_tags($data->getNom()));
        $message=htmlspecialchars(strip_tags($data->getMessage()));
        $mail=htmlspecialchars(strip_tags($data->getMail())); 
        $email = (new TemplatedEmail())
            ->from('mail@mail.com')
            ->to('adress@mail.com')
            ->subject("contact")
            ->htmlTemplate('email/contact.html.twig')
            ->context([
                'nom'=>$nom,
                'prenom'=>$prenom,
                'mail'=>$mail,
                'message'=>$message
            ]);
            $this->mailer->send($email);
        dd($data);    

    }

}

我将它连接到一个实体

/**
 * @ApiResource(
 *      itemOperations={},
 *      collectionOperations={"mail"={
 *          "method"="post", 
 *          "path"="/contacts/mailer",
 *          "controller"="App\Controller\ContactMailController",
 *          "openapi_context"={
 *              "summary"="Envoyer un mail à mail@test.com",
 *              "description"="Permet d'envoyer un e-mail à mail@test.com"
 *          }
 *      }},
 *      denormalizationContext={"disable_type_enforcement"=true}
 *)
 */
class Contact{
...

使用 Postman,一切顺利 但 与反应

const contactSubmit = async (event)=>{
      ...
                try{
                    axios.post("http://myapi.com/api/contacts/mailer",fields)
                    toast.info('votre message à bien été envoyé')
                    setFields({
                        "prenom": "",
                        "nom": "",
                        "mail": "",
                        "message": ""
                    })
                }catch(error){
                    //
                }  
     ...
    }

我有一个错误: Access to XMLHttpRequest at 'http://myapi.com/api/contacts/mailer' from origin 'http://mywebsite.com' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

但我的 .env 中有

###> nelmio/cors-bundle ###
CORS_ALLOW_ORIGIN=^.*$
###< nelmio/cors-bundle ###

最糟糕的是它被发送了, 它可以工作,但我不想看到显示的错误...

感谢您的帮助!

【问题讨论】:

  • 服务器配置(apache/nginx)中是否也有标头?

标签: reactjs api symfony api-platform.com symfony5


【解决方案1】:

我解决了我的问题! 我的错误在控制器中,我没有做出回应

   ...
    ->context([
                'nom'=>$nom,
                'prenom'=>$prenom,
                'mail'=>$mail,
                'message'=>$message
            ]);
            $this->mailer->send($email);
        // my mistake
        return new Response("sent"); 

【讨论】:

    猜你喜欢
    • 2021-02-04
    • 2022-11-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2021-04-10
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多