[英]angular 2 http post issue
i am getting error , when i am trying to sent post request to my server 当我尝试将发布请求发送到服务器时,出现错误
how to solve this ? 如何解决呢?
error 错误
XMLHttpRequest cannot load http://localhost/posttest.php .
XMLHttpRequest无法加载http://localhost/posttest.php 。 Request header field Content-Type is not allowed by Access-Control-Allow-Headers in preflight response.
飞行前响应中的Access-Control-Allow-Headers不允许请求标头字段Content-Type。
php code http://localhost/posttest.php PHP代码 http://localhost/posttest.php
header("Access-Control-Allow-Origin: *");<br>
$data = array("token"=>"","l_date"=>"");<br>
$data_json = json_encode($data);<br>
echo $data_json;
angular 2 http post code 角2 http邮政编码
import {Injectable} from '@angular/core';
import { Http, RequestOptions,Headers,Response } from '@angular/http';
import 'rxjs/add/operator/map';
@Injectable()
export class PostsService{
constructor(private http:Http){
console.log("post service initialized ... ");
}
getPosts(id:number){
return this.http.get('https://jsonplaceholder.typicode.com/posts/'+id).map(res=>res.json());
}
postexample(){
var json=JSON.stringify({milla:"hi",login2:"milla"});
var param="json="+json;
let headers1 = new Headers({ 'Accept': 'application/json','Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers1 });
let body = JSON.stringify(json);
return this.http.post('http://localhost/posttest.php', body, options ).map((res: Response) => res.json());
}
}
in php add the another access control headers: 在php中添加另一个访问控制标头:
http://localhost/posttest.php
header("Access-Control-Allow-Origin: *");
header("Access-Control-Allow-Headers: Origin, X-Requested-With, Content-Type, Accept");
$data = array("token"=>"","l_date"=>"");
$data_json = json_encode($data);
echo $data_json;
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.