角度没有得到json对象的响应

时间:2018-01-19 18:30:36

标签: json

我想在离子3 html中显示响应,但角度没有在json中得到它

这是m代码。

home.ts code

import { Component } from '@angular/core';
import { NavController } from 'ionic-angular';
import { Http } from '@angular/http'; 

import 'rxjs/Rx';

@Component({
 selector: 'page-home',
 templateUrl: 'home.html'
})

export class HomePage {
 data:any = {};

 constructor(public navCtrl: NavController, public http: Http) {
 this.data.name = '';
 this.data.id = '';
 this.data.response = '';

 this.http = http;
 }

 submit() {
 var link = 'http://127.0.0.1/pg/api.php';
 var myData = JSON.stringify({id: this.data.id, name: this.data.name});

 this.http.post(link, myData)
 .subscribe(data => {
 this.data.response = data["_body"]; 
 }, error => {
 console.log("Oooops!");
 });
 }
}

和home.html代码

<ion-card-content>
<b>{{data.response.id}}</b>
<b>{{data.response.name}}</b>
</ion-card-content>

我的php api看起来像

<?php

    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
    }

    // Access-Control headers are received during OPTIONS requests
    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);
    }


        $postdata = file_get_contents("php://input");

        $request = json_decode($postdata);
        $uid = $request->id;
        $unm = $request->name;
        $ob->id = $uid;
        $ob->name = $unm;


            echo json_encode($ob);



?>

但它没有显示任何内容。 api返回与json格式相同的输入数据,但它没有显示在frontend.whats我的代码中的问题。 有什么建议 ?

0 个答案:

没有答案