在我的Angular 2应用中,我在服务到组件和模板中发送一个变量,我有一个值我得到[对象]而不是字符串。
import { Injectable } from '@angular/core';
@Injectable()
export class FormService {
aForm: string[] = ['test1', 'test2'];
getData() {
return this.aForm;
}
ngOnInit(): void {
this.formServiceData = this.FormService.getData()
this.name = (this.formServiceData[0]);
}
您应该在将JSON发送到PHP服务
之前将其串起JSON.stringify(yourObject);
在您的PHP服务中,您可以使用json_decode()
函数解码JSON
<?php
$json = '{"a":1,"b":2,"c":3,"d":4,"e":5}';
var_dump(json_decode($json)); // Will return objects
var_dump(json_decode($json, true)); // Will return assoc array
?>
在发送带有帖子的变量之前,我需要呼叫 value variable.value php文件中的值:
$json = file_get_contents('php://input');
$params = json_decode($json);
$variable = $params->variable ;
我的问题与发送字符串数组而不是JSON连接。