M
M
ms23452016-01-13 16:49:34
JavaScript
ms2345, 2016-01-13 16:49:34

How to send ajax request in yii 2 and not get 400 (Bad Request) error?

Good day, I have a small problem.
I'm trying to send a POST request to the server via ajax in the yii 2 framework. I
created a separate controller for such requests. When trying to send any data to the controller:

url: "/ajax/message,
type: "POST",
data: "message=123123123"

An error is displayed in the console " POST http://site.ru/ajax/message 400 (Bad Request)".
The controller itself is working and when you try to access it directly or replace the method with GET, everything works as it should. Therefore, the conclusion is that the error is in the POST method.

Even if I send a request without data:
url: "/ajax/message,
type: "POST"


I really ask for help! Been sitting with this all day today.

Answer the question

In order to leave comments, you need to log in

2 answer(s)
T
The Dragger, 2016-01-13
@ms2345

Either add crf token or disable it.

J
Jenya Jenya, 2016-01-13
@noganno

Look at the rules of the router, maybe you have a different url there? For example, /ajax/message/
Try to manually enter this URL in the browser, /ajax/message, do you have such a page at all?

Didn't find what you were looking for?

Ask your question

Ask a Question

731 491 924 answers to any question