Warning: file_get_contents(/data/phpspider/zhask/data//catemap/1/php/263.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
无法发布到php脚本2_Php_Http_Angular_Typescript - Fatal编程技术网

无法发布到php脚本2

无法发布到php脚本2,php,http,angular,typescript,Php,Http,Angular,Typescript,我试图使用一个对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 '../serv

我试图使用一个对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
中,将调用my
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());
    }
}
从'@angular/core'导入{Injectable};
从'@angular/Http'导入{Http,Response,Headers,RequestOptions};
从“../definition/Trip”导入{Trip};
从'rxjs/Rx'导入{Observable};
导入'rxjs/add/operator/map';
导入“rxjs/add/operator/catch”;
@可注射()
导出类DbService{
构造函数(私有http:http){}
private dbUrl='../api/db_api.php';//指向web api的URL
getTrips(名称):可观察{
let headers=新的头({'Content-Type':'application/json'});
let options=newrequestoptions({headers:headers});
让body=JSON.stringify(name);
返回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文件中硬编码数据

谢谢你的帮助


关于

您的代码的问题是您正在使用相对url调用this.http.post。如果您想在本地运行,首先必须在系统中安装Apache web服务器和php。您的完整项目应该像我们所有php项目一样,都在ApacheWeb目录下

private dbUrl = '../apis/db_api.php';  // URL to web API
您的私有dbUrl错误。php文件只在服务器端工作。所以在localhost之后应该有文件位置的完整地址。像这样

 var Path="http://localhost/amit/login/server/login.php";
在url中添加localhost,然后包括db_api文件的位置。如果您已将代码上载到某个服务器上,请使用该服务器的地址代替localhost。您正在调用一个应该转到服务器的http请求

因此,您的dbUrl应该类似于私有dbUrl=''

@Injectable()
导出类DbService{
构造函数(私有http:http){}
私有dbUrl=http://localhost:3000/tripahead/apis/db_api.php“;res.json());
}
}

url不应该是“
”../api/db_api.php”
?很抱歉,即使使用.php,我也忘了将其更改回原来的url。这是同一个错误,我更新了问题和错误图片。我之前有完整的路径,但它不起作用,但是当我读到你的答案时,我意识到我的php文件没有在任何服务器或xamp或项目中的任何东西上运行,所以这可能是个问题,对吗?先生,如果你想在本地运行,首先你必须在系统中安装Apache web服务器和php。和我们所有的php项目一样,您的整个项目应该在ApacheWeb目录下。然后它将工作:)您得到的错误是404文件未找到。是的,我将php文件移动到我的ampps目录中,我的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());
    }
}