Ich baue eine mobile App, um News-Feed anzuzeigen. In meiner App sollte man einen Status veröffentlichen können.Angular2 - POST-Anfrage an Server senden
Der Status wird per POST-Methode an den PHP-Server gesendet.
Jetzt ist mein Problem PHP liest nicht die POST-Anfrage, die ich mit angular2 gesendet.
Dies ist mein Code:
form.html
<form class="sample-form post-form" [formGroup]="post_form" (ngSubmit)="createStatus()">
<ion-item>
<ion-textarea rows="7" placeholder="What's happening?'" formControlName="status"></ion-textarea>
</ion-item>
<section class="form-section">
<button ion-button block class="form-action-button create-post-button" type="submit" [disabled]="!post_form.valid">Post</button>
</section>
</form>
form.ts
import { Component } from '@angular/core';
import { NavController, AlertController } from 'ionic-angular';
import { Validators, FormGroup, FormControl } from '@angular/forms';
import { Http, Headers } from '@angular/http';
import 'rxjs/add/operator/map';
@Component({
selector: 'form-page',
templateUrl: 'form.html'
})
export class FormLayoutPage {
section: string;
post_form: any;
url: string;
headers: Headers;
constructor(public nav: NavController, public alertCtrl: AlertController, public http: Http) {
this.headers = new Headers();
this.headers.append("Content-Type", "application/x-www-form-urlencoded");
this.section = "post";
this.post_form = new FormGroup({
status: new FormControl('', Validators.required),
});
}
createStatus(){
console.log(this.post_form.value);
this.url = "https://domain.com/mobileREST/poststatus.php";
this.http.post(this.url, this.post_form.value, { headers: this.headers})
.map(res => res.json())
.subscribe(res => {
console.log(res);
},
err => {
console.log(err);
})
}
}
poststatus.php
<?php
header('Access-Control-Allow-Origin: *');
header('Content-Type: application/json');
$status = $_POST["status"];
echo json_encode($status);
?>
Ich kann nicht scheinen, den Fehler hier zu finden. Wirklich zu schätzen Ihre Hilfe
Hallo, ein neuer Winkel Benutzer hier Datei hinzufügen. Können Sie erklären, warum Sie _ für die private Variable verwendet haben? Ist es notwendig (sowohl für _http als auch für _buildParams)? – iWillGetBetter
@iWillGetBetter Es ist nicht notwendig, es ist nur eine Konvention für private Methoden und Variablen. –