I'm new in Angular, and i'm developing a login auth service, but i'm gettin some troubles to make the post data for an backend developed in CodeIgniter.
I'm sending: username and userpassword, to URL: http://192.168.1.162/advance-managemente_2.1/login
Follow my code:
import { Injectable } from '@angular/core';
import { HttpClient, HttpHeaders } from '@angular/common/http';
import { UserData } from './login/user.model';
@Injectable({
providedIn: 'root'
})
export class AuthService {
constructor(private http: HttpClient) { }
userData: UserData;
getUserDetails(username, userpassword) {
console.log(username, userpassword);
const userdata = JSON.stringify({user: {login: username, password: userpassword}});
const headers = new HttpHeaders({'Content-Type': 'application/x-www-form-urlencoded'});
console.log(userdata);
// post these details to API server return user info if correct.
return this.http.post(`http://192.168.1.162/advance-management_2.0/login`, userdata, {headers: headers, observe: 'response'})
.subscribe(res => {
console.log(res);
},
err => {
console.error(err);
});
}
}
And my console: Note: my variables with username and userpassword is working.
And in codeigniter's backend project is setted these headers:
header("Access-Control-Allow-Origin: *");
header('Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Access-Control-Allow-Origin');
header('Access-Control-Allow-Methods: POST, GET, OPTIONS, PUT, DELETE');
And the function os codeigniter is:
function login(){
$json = $this->input->post('user');
echo json_encode($json);
}
If you observe, my login function have an ECHO with json_encode of i'm sending to her. But, in console, the body is returning NULL.
I'd like to know what I might be doing wrong, and how to fix it, because it was for him to return a JSON, but it does not seem to be identifying my POST.
Thanks to anyone who can help, Have a nice day!