L
L
lilwings2019-12-23 16:07:17
JavaScript
lilwings, 2019-12-23 16:07:17

Yii2 ajax js request not finding action?

routes:

'urlManager' => [
    'enablePrettyUrl' => true,
    'showScriptName' => false,
    'rules' => [
        '' => 'site/index',
        'sendMail' => 'site/sendMail',
    ],
]<img src="https://habrastorage.org/webt/5e/00/bb/5e00bbe8e081b833076907.png" alt="image"/>


action:

public function actionSendMail() {
    $dataMessage = [
        'name'    => trim( strip_tags( Yii::$app->request->post('name', '') ) ),
        'email'   => trim( strip_tags( Yii::$app->request->post('email', '') ) ),
        'number'  => trim( strip_tags( Yii::$app->request->post('number', '') ) ),
        'message' => trim( strip_tags( Yii::$app->request->post('message', '') ) )
    ];

    $miler = new SendMail();

    return $miler->SendMail($dataMessage);
}


js

fetch('/sendMail', {
    method: 'post',
    body: new FormData(form)
}).then((response) => {
    response.text().then((data) => {
        console.log(data);
    });
});


5e00bbe84ec27079077637.png

Answer the question

In order to leave comments, you need to log in

2 answer(s)
S
Sergey, 2019-12-23
@lilwings

Write the correct action in the rules: 'sendMail' => 'site/send-mail'

A
Arman, 2019-12-23
@Arik

https://developer.mozilla.org/ru/docs/Web/API/Fetc...

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question