我正在尝试使用一个对php脚本的post请求,该脚本包含我需要的数据。
home.component.ts
import { Component, OnInit } from '@angular/core';
import { UserComment } from '../definition/user.comment';
import { HomeService } from '../services/home.service';
import { DbService } from '../services/db.service';
import { Trip } from '../definition/trip';
@Component({
templateUrl: './templates/home.html',
providers: [HomeService, DbService]
})
export class HomeComponent implements OnInit {
user_comments: UserComment[];
trips: Trip[];
constructor(private homeService: HomeService, private dbService: DbService) { }
getUserComments(): void {
this.homeService.getData().then(user_comments => this.user_comments = user_comments);
}
ngOnInit(): void {
this.getUserComments();
console.log(this.dbService.getTrips('test').subscribe());
}
}在ngOnInit中,我的DbService中的getTrips()函数将被调用,如下所示。
import { Injectable } from '@angular/core';
import { Http, Response, Headers, RequestOptions} from '@angular/http';
import { Trip } from '../definition/trip';
import { Observable } from 'rxjs/Rx';
import 'rxjs/add/operator/map';
import 'rxjs/add/operator/catch';
@Injectable()
export class DbService {
constructor (private http: Http) {}
private dbUrl = '../apis/db_api.php'; // URL to web API
getTrips(name): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
let body = JSON.stringify(name);
return this.http.post(this.dbUrl, body, options).map((res: Response) => res.json());
}
}这将是php文件。
<?php
header("Access-Control-Allow-Origin:*");
header('Access-Control-Allow-Headers: X-Requested-With');
header('Content-Type: application/json');
$data = array(
'id' => 1,
'user_id' => 1,
'route_name' => 'test',
'route_info' => 'test info',
'rating' => 2.5,
'coordinates' => '9838e901',
'is_active' => 1,
'deleted_at' => NULL
);
echo json_encode($data);
?>当我这样做时,我在控制台中得到以下错误

这将是项目结构

奇怪的是,当我将post改为get时,我得到了php文件的JSON字符串。
我们的想法是创建一个与本地数据库通信的php文件来返回数据,但现在我只是将数据硬编码到php文件中。
会很感谢你的帮助
问候
发布于 2017-01-14 18:16:08
你的代码的问题是你正在使用相对url.first调用this.http.post,如果你想在本地运行它,你必须在你的系统中安装Apache web server和php。和我们所有的php项目一样,完整的项目应该在Apache web目录下。
private dbUrl = '../apis/db_api.php'; // URL to web API您的私有dbUrl错误。php文件只能在服务器端运行。因此,在localhost之后应该有文件位置的完整地址。像这样
var Path="http://localhost/amit/login/server/login.php";在url中添加本地主机,然后包含db_api文件的位置。如果您已经将代码上传到某个服务器上,则使用该地址代替localhost。您正在调用一个http请求,该请求应发往服务器。
所以你的dbUrl应该是私有的dbUrl = 'http://localhost:3000/tripahead/apis/db_api.php';
@Injectable()
export class DbService {
constructor (private http: Http) {}
private dbUrl = 'http://localhost:3000/tripahead/apis/db_api.php'; <-- location of your server php file location.
getTrips(name): Observable<Response> {
let headers = new Headers({'Content-Type': 'application/json'});
let options = new RequestOptions({headers: headers});
let body = JSON.stringify(name);
return this.http.post(this.dbUrl, body, options).map((res: Response) => res.json());
}
}https://stackoverflow.com/questions/41648603
复制相似问题