douxi0098 2018-04-15 21:16
浏览 68
已采纳

无法处理来自PHP服务和JSON的Ionic HTTP请求

I'm trying to build an Angular-Ionic app, which gets information from a central database via http request information. These are built by PHP in the form of JSON. I also get the information back but can not process it.

My JSON looks like this:

[
   {0: {id: 1, title: 'TITLE', image:'TITEL'},
   {1: {id: 2, title: 'TITLE', image:'TITEL'},
   {2: {id: 3, title: 'TITLE', image:'TITEL'}
]

This is my PHP-Code:

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, PUT, DELETE, OPTIONS");

if (isset($_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']))
    header("Access-Control-Allow-Headers: 
    {$_SERVER['HTTP_ACCESS_CONTROL_REQUEST_HEADERS']}");
 }
$mysqli = new mysqli("[DATABASE_URL]", "[DATABASE_USER]", "[DATABASE_PWD]", "[DATABASE]");
$query = "SELECT id, title, image FROM database ORDER BY title ASC";
$dbresult = $mysqli->query($query);

while($row = $dbresult->fetch_array(MYSQLI_ASSOC)){

    $data[] = array(
        'id' => $row['id'],
        'title' => $row['title'],
        'image' => $row['image']
    );
}

if($dbresult){
    $result = json_encode(utf8ize($data),JSON_FORCE_OBJECT);
} else {
    $result = "{success:false}";
}
echo $result;


function utf8ize($d) {
    if (is_array($d)) {
        foreach ($d as $k => $v) {
            $d[$k] = utf8ize($v);
        }
    } else if (is_string ($d)) {
        return utf8_encode($d);
    }
    return $d;
 }

I would like to build a list with the obtained data in Angular / Ionic. I use Ionic 3 on the latest version. Http and HttpClient, as well as HttpProvider did not bring any result:

<ion-card *ngFor="let item of newsData">
  <img src="../../assets/imgs/{{ item.image }}.jpeg"/>
  <div class="card-title">{{ item.title }}</div>
  <div class="card-subtitle">{{item.id}} Rezepte</div> 
</ion-card>

How can I achieve that?

  • 写回答

1条回答 默认 最新

  • douweng7308 2018-04-16 07:43
    关注

    You have to assign the response from your http request to a variable and than just do it the way you did.

    If you give more informations about what you did in ionic to get the response we can see what's the mistake here.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 mmo能不能做客户端怪物
  • ¥15 osm下载到arcgis出错
  • ¥15 Dell g15 每次打开eiq portal后3分钟内自动退出
  • ¥200 使用python编写程序,采用socket方式获取网页实时刷新的数据,能定时print()出来就行。
  • ¥15 matlab如何根据图片中的公式绘制e和v的曲线图
  • ¥15 我想用Python(Django)+Vue搭建一个用户登录界面,但是在运行npm run serve时报错了如何解决?
  • ¥15 QQ邮箱过期怎么恢复?
  • ¥15 登录他人的vue项目显示服务器错误
  • ¥15 (标签-android|关键词-app)
  • ¥15 comsol仿真压阻传感器