Reputation: 21
In my Angular 2 app I'm sending a variable from service to component and in the template, I have a value, but when I trying to send this variable to a PHP script with POST I get [object Object] instead of a string.
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]);
}
Upvotes: 0
Views: 90
Reputation: 21
My problem was connected with sending a string array instead of JSON.
Upvotes: 0
Reputation: 21
Before sending a variable with POST I need call value variable.value in php file:
$json = file_get_contents('php://input');
$params = json_decode($json);
$variable = $params->variable ;
Upvotes: 0
Reputation: 4884
You should stringify the JSON before you send it to your php service
JSON.stringify(yourObject);
In your PHP service you can decode the JSON using json_decode()
function
<?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
?>
Upvotes: 3