Closed madsongr closed 7 years ago
Hi @madsongr, I tested the HTTP module while using nativescript-sdl-examples-ng HTTP post example and everything seems to work as expected. Regarding that, the problem seems to be related to the backend service and the way it is handling the request.
Please verify, whether the request params are handled properly in the backend service.
Thank you for your attention @tsonevn
My backend was good. I made some changes and it worked:
service.ts
import { Injectable } from "@angular/core";
import { Http, Headers, Response, RequestOptions } from "@angular/http";
import { Observable as RxObservable } from "rxjs/Observable";
import "rxjs/add/operator/map";
let headers = new Headers(
{
'Content-Type': 'application/x-www-form-urlencoded'
});
@Injectable()
export class MyHttpPostService {
private serverUrl = "http://website.com/loginsystem/users/insert.php";
constructor(private http: Http) { }
postData(data: any) {
return this.http.post(this.serverUrl, data, {headers:headers, method:"POST"}).map(res => res.json());
}
}
signup.ts
import { Component, ViewChild, ElementRef } from "@angular/core";
import { TNSCheckBoxModule } from 'nativescript-checkbox/angular';
import { Color } from "color";
import { Http, Headers, Response, RequestOptions } from "@angular/http";
import { Observable as RxObservable } from "rxjs/Observable";
import "rxjs/add/operator/map";
import { MyHttpPostService } from '../../service';
@Component({
selector: "signup",
styleUrls: ['./components/signup/signup.css'],
templateUrl: "./components/signup/signup.html",
providers: [MyHttpPostService]
})
export class SignupComponent {
public message: string = ""
constructor(private http: Http, private post: MyHttpPostService){
}
myCheckColor = new Color("#f68533");
terms:any;
public checkedChange(modelRef) {
if(modelRef.checked === true){
console.log("checkedChange:", modelRef.checked);
this.terms = modelRef.checked;
}else {
console.log("é false:", modelRef.checked);
this.terms = modelRef.checked;
}
}
userData = {"name": "", "uname": "", "celular": "", "endereco": "", "numero": "", "bairro": "",
"cidade": "", "cep": "", "senha": "", "confirmar": "", "termos": this.terms}
public cadastrar(){
console.log(JSON.stringify(this.userData));
this.post.postData(this.userData)
.subscribe(res => {
this.message = res[0].NOME;
console.log(this.message);
},
err => {
console.log("ERROR!: ", err);
});
}
}
signup.html:
<Page xmlns="http://schemas.nativescript.org/tns.xsd"
xmlns:CheckBox="nativescript-checkbox"
xmlns:RL="nativescript-ripple" class="page">
<Page.actionBar>
<ActionBar title="Cadastre-se" icon="" class="action-bar">
</ActionBar>
</Page.actionBar>
<ScrollView>
<StackLayout class="p-16">
<Label text="Nome"></Label>
<TextField text="" [(ngModel)]="userData.name" editable="true"></TextField>
<Label text="Sobrenome"></Label>
<TextField text="" [(ngModel)]="userData.uname" editable="true"></TextField>
<Label text="Celular"></Label>
<TextField text="" [(ngModel)]="userData.celular" editable="true"></TextField>
<Label text="Endereço"></Label>
<TextField text="" [(ngModel)]="userData.endereco" editable="true"></TextField>
<Label text="Número"></Label>
<TextField text="" [(ngModel)]="userData.numero" editable="true"></TextField>
<Label text="Bairro"></Label>
<TextField text="" [(ngModel)]="userData.bairro" editable="true"></TextField>
<Label text="Cidade"></Label>
<TextField text="" [(ngModel)]="userData.cidade" editable="true"></TextField>
<Label text="Cep"></Label>
<TextField text="" [(ngModel)]="userData.cep" editable="true"></TextField>
<Label text="Senha"></Label>
<TextField text="" [(ngModel)]="userData.senha" editable="true"></TextField>
<Label text="Confirmar senha"></Label>
<TextField text="" [(ngModel)]="userData.confirmar" editable="true"></TextField>
<Label class="termos" text="Termos e condições" horizontalAlignment="center"></Label>
<CheckBox #termos text="Li e aceito os termos e condições"
fillColor="{{ myCheckColor }}" checked="false" class="checkbox"
(checkedChange)="checkedChange(termos)" [(ngModel)]="userData.termos" editable="true"></CheckBox>
<RL:Ripple rippleColor="#df6107">
<Button text="Cadastrar" (tap)="cadastrar()"></Button>
</RL:Ripple>
</StackLayout>
</ScrollView>
</Page>
This thread has been automatically locked since there has not been any recent activity after it was closed. Please open a new issue for related bugs.
I am trying to post data to my database but nothing is happening. I got no messages in console.log() or errors.
signup.ts
service.ts
app.module.ts