我是Angular CLI的初学者,我使用了http.post登录api:'http://localhost/appointjobs/index.php/admin_api/index',但是当设置{我没有在服务器端(codeigniter / php)获取发布数据时{1}}。我在登录服务中使用了以下代码,并在使用'content-type:application/json'
而不是'application / json'时获取了帖子数据。
'application/x-www-form-urlencoded'
AuthService.ts文件:
DataService.ts file:
import { BadInputError } from './../common/bad-input-error';
import { error } from 'selenium-webdriver';
import { AppError } from './../common/app-error';
import { Observable } from 'rxjs/Observable';
import { Http, ResponseOptionsArgs,RequestOptionsArgs,Headers,RequestOptions } from '@angular/http';
import { Injectable } from '@angular/core';
import 'rxjs/add/operator/catch';
import 'rxjs/add/operator/map';
import 'rxjs/add/observable/throw';
import { NotFoundError } from '../common/not-found-error';
import { Response } from '@angular/http/src/static_response';
import { HttpHeaders } from '@angular/common/http';
@Injectable()
export class DataService {
constructor(private http:Http) {
}
getWhere(url,resource){
let headers= new Headers();
//headers.append('Access-Control-Allow-Origin:','*');
headers.append('Accept','text/plain');
headers.append('content-type','application/json');
//headers.append('content-type','application/x-www-form-urlencoded');
let option= new RequestOptions({headers:headers});
return this.http.post(url,JSON.stringify(resource),option)
.map(response=>response.json())
.catch(this.handleError);
}
}
答案 0 :(得分:2)
使用FormData将您的数据发送到php
将您的服务更改为以下
getWhere(url,resource){
const formData: FormData = new FormData();
formData.append('data', JSON.stringify(resource));
let headers= new Headers();
headers.append('Accept', 'application/json');
return this.http.post(url,formData, { headers: headers })
.map(response=>response.json())
.catch(this.handleError);
}
}
并在你的php中
print_r($_POST['data']); // gives you the json
使用
json_decode($_POST['data']) // converts your json string into object