Javascript 动态更新JSON数据
我正试图从php backend.JSON数据的第一个属性获取JSON数据到我的角度前端 “Javascript 动态更新JSON数据,javascript,php,json,angular,rest,Javascript,Php,Json,Angular,Rest,我正试图从php backend.JSON数据的第一个属性获取JSON数据到我的角度前端 “stock\u price”随机更改,但JSON数据的time属性应以秒为单位递增。 也就是说 { stock_price: 19, time: 1 } 接下来应该是 { stock_price: 26, time: 2 } 像那样 当我使用browser/postman尝试这个php脚本时,它会在每个页面刷新/发布请求中更新,如下所示 我预料到了 但当我在Angular应用程序中对其进行控制台操作
stock\u price
”随机更改,但JSON数据的time属性应以秒为单位递增。
也就是说
{
stock_price: 19,
time: 1
}
接下来应该是
{
stock_price: 26,
time: 2
}
像那样
当我使用browser/postman尝试这个php脚本时,它会在每个页面刷新/发布请求中更新,如下所示
我预料到了
但当我在Angular应用程序中对其进行控制台操作时,time
属性不会改变<代码>时间保持为1
为了得到更新的json回复,我可以做什么
php文件
<?php
header('Content-Type: application/json');
session_start();
$time = isset($_SESSION['time']) ? $_SESSION['time'] : 0;
$stock_price = rand(10,100);
$arr['stock_price'] = $stock_price;
$arr['time'] = ++$time;
echo json_encode($arr);
$_SESSION['time'] = $time;
?>
export class AppComponent {
ngOnInit(){
this._helper.helperHelp()
.subscribe(res => {
console.log(res);
})
}
constructor(private _helper:HelperService){
setInterval(()=>{
this.ngOnInit(); },4000);
}
}
helperHelp(time) {
return this._http.get(`http://localhost/restPHP/restphp.php?time=${time}`).map(result =>result);
}
服务等级
export class HelperService {
constructor(private _http:HttpClient) { }
helperHelp(){
return this._http.get("http://localhost/restPHP/restphp.php")
.map(result =>result);
}
}
您必须从Angular通过
时间。如果在URL中使用POST
,则可以将其作为body
发送(如果是GET
)
PHP
$time = isset($_GET['time']) ? $_GET['time'] : 0; // change to $_POST in case of POST
角度分量
export class AppComponent {
stockData;
constructor(private _helper:HelperService){
}
ngOnInit(){
this.getDataFromServer();
this.scheduleInterval();
}
scheduleInterval() {
setInterval(this.getDataFromServer, 4000);
}
getDataFromServer() {
let time = this.stockData ? this.stockData.time : 0;
this._helper.helperHelp(time).subscribe(res => { //use time in your service
this.stockData = res;
console.log(this.stockData);
});
}
}
服务
<?php
header('Content-Type: application/json');
session_start();
$time = isset($_SESSION['time']) ? $_SESSION['time'] : 0;
$stock_price = rand(10,100);
$arr['stock_price'] = $stock_price;
$arr['time'] = ++$time;
echo json_encode($arr);
$_SESSION['time'] = $time;
?>
export class AppComponent {
ngOnInit(){
this._helper.helperHelp()
.subscribe(res => {
console.log(res);
})
}
constructor(private _helper:HelperService){
setInterval(()=>{
this.ngOnInit(); },4000);
}
}
helperHelp(time) {
return this._http.get(`http://localhost/restPHP/restphp.php?time=${time}`).map(result =>result);
}
在会话中添加新值。当前,您每次都在添加0
。
在任何PHP文件中添加以下代码,并尝试每次重新加载
session_start();
$time = isset($_SESSION['time']) ? $_SESSION['time'] : 0;
$arr['time'] = ++$time;
$_SESSION["time"] = $time;
echo json_encode($arr);
send withcredential=true在post/get requestMove session set之前echo
@u\u mulder我没有得到它,session set意味着,isset
或session\u start
?它意味着$\u session['time]=$time代码>@u_mulder不起作用,但我仍然得到1::如何将时间
添加到我的服务连接功能帮助
?我在angular方面没有太多经验:)现在就是这样helperHelp(){返回这个http://localhost/restPHP/restphp.php).map(result=>result);}
像这样使用。\u http.get(http://localhost/restPHP/restphp.php?time=${time}
)我收到一个错误,错误类型错误:无法读取未定义的属性“helperHelp”
原因是什么?它不应该给出。您是否正在提供商中添加HelperService
?让我们来看看。我试过了,但这只适用于浏览器,不适用于Angular应用程序