I'm using express as my back-end and I want to simply post JSON to it. I can get the request to the server but when it arrives it's empty and I can't find my data... Here is my code:
import {Injectable} from "@angular/core";
import { HTTP } from '@ionic-native/http';
import {User} from "./user";
import {Config} from "../config";
import {Observable} from "rxjs/Rx";
import 'rxjs/add/operator/map';
@Injectable()
export class UserService {
constructor(private _http: HTTP) {}
//Makes http request to valid username and password
login(user: User) {
//Creates header to tell server that it is getting json
//Sets address to request
var address = Config.apiUrl + "login/";
//Creates JSON object that contains the data
let data = JSON.stringify({
username: user.username,
password: user.password
});
console.log(data);
//Make request to server
return this._http.post(address, data, {"Content-Type":"application/json"})
.then(res => {
return(res);
})
.catch(error => {
return Observable.throw(error);
})
}
}
Thank you! I'd also like to note that I've seen other people say that changing the header to x-www-form-urlencoded will fix it, but I want to send my information with JSON as I plan on using JSON web tokens in the future.