Http.post 在 Angular4 cli 中 header 中设置 'application/json' 时不工作
Http.post not working when 'application/json' set in header in Angular4 cli
我是 Angular CLI 的初学者,我使用登录名 api:'http://localhost/appointjobs/index.php/admin_api/index' 使用 http.post 但是,我没有得到 post 设置 'content-type:application/json'
时服务器端的数据 (codeigniter/php)。下面是我在登录服务中使用的代码,当我使用 'application/x-www-form-urlencoded'
而不是 'application/json' 时也得到了 post 数据。
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);
}
}
AuthService.ts 文件:
import { DataService } from './data.service';
import { Injectable } from '@angular/core';
@Injectable()
export class AuthService{
private url = 'http://localhost/appointjobs/index.php/admin_api/index';
constructor(private dataService:DataService) {
}
signIn(params:HTMLInputElement){
this.dataService.getWhere(this.url,params)
.subscribe(response=>{
console.log(response);
});
}
}
使用 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
我是 Angular CLI 的初学者,我使用登录名 api:'http://localhost/appointjobs/index.php/admin_api/index' 使用 http.post 但是,我没有得到 post 设置 'content-type:application/json'
时服务器端的数据 (codeigniter/php)。下面是我在登录服务中使用的代码,当我使用 'application/x-www-form-urlencoded'
而不是 'application/json' 时也得到了 post 数据。
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);
}
}
AuthService.ts 文件:
import { DataService } from './data.service';
import { Injectable } from '@angular/core';
@Injectable()
export class AuthService{
private url = 'http://localhost/appointjobs/index.php/admin_api/index';
constructor(private dataService:DataService) {
}
signIn(params:HTMLInputElement){
this.dataService.getWhere(this.url,params)
.subscribe(response=>{
console.log(response);
});
}
}
使用 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