I am newbie to yii and i am stuck with one issue in yii. This is my first topic in yii forum.
I have created REST API which uses HTTP basic Authentication with "Content-Type: application/json" using Yii1.1. I have tested it in Postman rest client, It works fine as i expect.
Here is the example of HTTP request:
POST /userApi/create HTTP/1.1
Host: myhosting.com
Authorization: Basic YoTrrTasdfsdfdfsdfs3VtYXI6QXNoaXA==
Content-Type: application/json
{
"customer_email":"customer1@gmail.com",
"username":"test123",
"password":"12345678",
"pwd_comparison":12345678,
"role_id":2,
"customer_mobile":9876543211,
"first_name":"test"
}
And it gives me expected response,
{
"customer_email": "customer1@gmail.com",
"username": "test123",
"password": "dFcrYzl3TlhCM1g2SXNDZXRFeUp1dz09",
"customer_mobile": 9876543211,
"first_name": "test",
"created": "2017-03-10 20:54:33",
"user_id": "63"
}
So I hope there is not any issue with code.
Now when i am trying to integrate same thing in my code, it does not work as it should be.
My code is in simple javascript as below:
<button id="btnCreateUser" onclick="create_user();">Create User</button>
<script type="text/javascript">
function create_user(){
var data = JSON.stringify({
"customer_email": "customer1@gmail.com",
"username": "test123",
"password": "12345678",
"pwd_comparison": 12345678,
"role_id": 2,
"customer_mobile": 9876543211,
"first_name": "test"
});
var xhr = new XMLHttpRequest();
xhr.withCredentials = true;
xhr.addEventListener("readystatechange", function () {
if (this.readyState === 4) {
console.log(this.responseText);
}
});
xhr.open("POST", "myhosting/userApi/create");
xhr.setRequestHeader("authorization", "Basic YoTrrTasdfsdfdfsdfs3VtYXI6QXNoaXA==");
xhr.setRequestHeader(‘Content-Type’, ‘application/json; charset=UTF-8’);
xhr.send(data);
}
</script>
After running this code click on button "create user" to make request. (Make sure to keep developer tool inspect element to see the request in "Net" panel. (when i look at request in inspect it shows me "Request Method : OPTIONS", even though i am trying POST request, i did googling but didnt find appropriate solution)
- My Issues are:
1)How to handle "OPTIONS" request in method in Yii1.1 ?
-
How to use to apply filter like "isPostRequest or isGetRequest, generally i do for post/get requests, how it should be for OPTIONS request ?
-
How to get requested params (which i passed using JSON in above example), generally i use below code to get posted json data using POST request:
$data = CJSON::decode(Yii::app()->request->getRawBody());
How it should be for OPTIONS request method ?
I am getting response like below,
{
"errors": {
"username": [
"Username cannot be blank."
],
"customer_email": [
"User Registration Email cannot be blank."
],
"password": [
"Password cannot be blank.",
"Password cannot be blank."
],
"pwd_comparison": [
"Repeat Password cannot be blank.",
"Repeat Password cannot be blank."
]
}
}
I know why it respond above because it doesnt get request params !
Can anyone tell me the solutions for my above queries ?
I appreciate your help. i spent to much time on this.
Note: i am using chrome browser.
Thanks,
Abnarola