在完成GET
Angular2
PHP
个node
请求的this quick example后,我继续获取php
个文件后,
错误304
我正在测试使用angular2
和app
作为GET
php
后端的区别。我有一个简单的CORS
请求,它试图从express app.js
文件中检索少量数据。
请求似乎没有问题,但文件没有提供给应用程序。
304 NOT MODIFIED已收到条件GET或HEAD请求 如果没有,那么会产生200 OK的回复 事实上,条件评估为假。
换句话说,
服务器不需要传输目标资源的表示,因为请求指示使请求有条件的客户端已经具有有效表示;因此,服务器重定向客户端以使用该存储的表示,就像它是200 OK响应的有效负载一样。
CORS
我在CORS
文件中允许PHP
。
我在console.log
文件中允许ng2
。
我如何克服这个问题?我至少希望<?php
header("Access-Control-Allow-Origin: *");
$data = array(
array('id' => '1','first_name' => 'Cynthia'),
array('id' => '2','first_name' => 'Keith'),
array('id' => '3','first_name' => 'Robert'),
array('id' => '4','first_name' => 'Theresa'),
array('id' => '5','first_name' => 'Margaret')
);
echo json_encode($data);
?>
来自import {Http, Response} from '@angular/http';
import {Injectable, Component } from '@angular/core';
@Component({
selector: 'my-app',
template: `<ul>
<li *ngFor="let person of data">
{{person.id}} - {{person.first_name}}
</li>
</ul>`
})
export class AppComponent {
private data;
constructor(private http:Http){
}
ngOnInit(){
this.getData();
}
getData(){
this.http.get('api.example.com/index.php')
.subscribe(res => this.data = res.json());
}
}
方的数据。
代码示例
PHP //在Apache端口80上运行@ api.example.com
getData()
Angular2 //在Express Server上运行@ localhost:4200
getData(){
let url = 'api.example.com/index.php';
let headers = new Headers({ 'Content-Type': 'application/json' });
let options = new RequestOptions({ headers: headers });
this.http.get(url, options)
.subscribe(res => {
this.data = res.json();
console.log(this.data);
});
}
我试图修改有效的list
函数。最近这个,
for
答案 0 :(得分:1)
我有类似的问题,但已经能够通过在我的api脚本上设置以下标题来修复它;
if (isset($_SERVER['HTTP_ORIGIN'])) {
header("Access-Control-Allow-Origin: {$_SERVER['HTTP_ORIGIN']}");
header('Access-Control-Allow-Credentials: true');
header('Access-Control-Max-Age: 86400'); // cache for 1 day}
//由于在OPTIONS请求期间收到了Access-Control标头
if ($_SERVER['REQUEST_METHOD'] == 'OPTIONS') {
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_METHOD']))
header("Access-Control-Allow-Methods: GET, POST, OPTIONS");
if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
header("Access-Control-Allow-Headers: {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
exit(0); }
希望有所帮助