我很沮丧,因为我一直在尽力解决这类问题,搜索文档和可能重复的问题。
我读过这个文档https://angular.io/docs/ts/latest/guide/server-communication.html,但这里没有用PHP发布数据的例子。
我要做的是检查数据库是否有未经批准的事务。它每3秒跑一次。
这是我得到的
我创建了我的服务[get-new-data.service.ts
]
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions } from '@angular/http';
import 'rxjs/Rx';
@Injectable()
export class GetNewDataService {
constructor(private http: Http) { }
private url = 'app/service/getDataService.php';
getData(bangko) {
let headers = new Headers('Content-Type', 'application/x-www-form-urlencoded'),
options = new RequestOptions({ headers: headers }),
bank = `bank=${bangko}`;
return this.http.post(this.url, bank, options).map(res => res.json());
}
}
在我的
app.module.ts
上,我导入了HttpModule-imports和GetNewDataService-providers。在我的组件上
import { Component, OnInit } from '@angular/core';
import { GetNewDataService } from '../service/get-new-data.service';
import { Observable } from 'rxjs/Rx';
@Component({
templateUrl: 'app/tpl-html/mandiri.component.html'
})
export class MandiriComponent implements OnInit {
constructor(private getDataService: GetNewDataService) { }
ngOnInit() {
setInterval(() => {
this.getDataService.getData('mandiri')
.subscribe(res => console.log(res));
}, 3000)
}
}
我不知道如何提出适当的请求,并将我的angular 2应用程序与mysql和php集成。
任何帮助都将不胜感激。谢谢:/
最佳答案
Post JSON from angular 2 to php
http://4dev.tech/2016/07/using-http-client-to-integrate-angular2-to-php/
var link = 'app/service/getDataService.php';
var data = JSON.stringify({username: this.data.username});
this.http.post(link, data)
.subscribe(data => {
this.data.response = data._body;
}, error => {
console.log("Oooops!");
});
关于php - 如何从Angular 2向PHP发送ajax请求[POST]?,我们在Stack Overflow上找到一个类似的问题:https://stackoverflow.com/questions/40233134/