yiisoft / yii2

Yii 2: The Fast, Secure and Professional PHP Framework
http://www.yiiframework.com
BSD 3-Clause "New" or "Revised" License
14.24k stars 6.91k forks source link

problem get data post message of restfull api #13826

Closed leehung-net closed 7 years ago

leehung-net commented 7 years ago

Hi all, I have using framework for my backend and create a module api restful. my frontend use angular-cli, when i make a request login as below: var data = "username=username2&password=123456";

var xhr = new XMLHttpRequest(); xhr.withCredentials = true;

xhr.addEventListener("readystatechange", function () { if (this.readyState === 4) { console.log(this.responseText); } });

xhr.open("POST", "http://esl.com/backend/web/api/user/login"); xhr.setRequestHeader("authorization", "Basic dXNlcm5hbWUyOjEyMzQ1Ng=="); xhr.setRequestHeader("content-type", "application/x-www-form-urlencoded"); xhr.setRequestHeader("cache-control", "no-cache");

xhr.send(data);

On Api I use code as below get post data: $post = \Yii::$app->request->post();

But all request had $post = array empty use postman is ok

Q A
Yii version 2.0
PHP version 7.0
Operating system window
samdark commented 7 years ago

What's in $_POST?

leehung-net commented 7 years ago

Hi samdark, $_POST= {array}[0]

samdark commented 7 years ago

Means no data was received by PHP.

samdark commented 7 years ago

How about Yii::$app->request->getRawBody()?

leehung-net commented 7 years ago

Hi samdark, Thank for answer. I resolve this issue, it is problem cross domain of apache.