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 seatunnel-web使用SQL组件时候后台报错,无法找到表格
  • ¥15 fpga自动售货机数码管(相关搜索:数字时钟)
  • ¥15 用前端向数据库插入数据,通过debug发现数据能走到后端,但是放行之后就会提示错误
  • ¥30 3天&7天&&15天&销量如何统计同一行
  • ¥30 帮我写一段可以读取LD2450数据并计算距离的Arduino代码
  • ¥15 飞机曲面部件如机翼,壁板等具体的孔位模型
  • ¥15 vs2019中数据导出问题
  • ¥20 云服务Linux系统TCP-MSS值修改?
  • ¥20 关于#单片机#的问题:项目:使用模拟iic与ov2640通讯环境:F407问题:读取的ID号总是0xff,自己调了调发现在读从机数据时,SDA线上并未有信号变化(语言-c语言)
  • ¥20 怎么在stm32门禁成品上增加查询记录功能