首页 > 解决方案 > 在 Ionic 4 中进行 AJX 调用时遇到问题

问题描述

我正在制作一个应用程序,它将使用 AJAX 请求获取 MySQL 数据并将一些数据发布到数据库中。这是一个问题,它没有在我的项目中进行 AJAX 调用

我正在使用 Ionic 4 和 PHP-MySQL 作为后端服务

import { Component, OnInit } from '@angular/core';
import * as $ from 'jquery';
import { data } from 'jquery';

@Component({
  selector: 'app-home',
  templateUrl: 'home.page.html',
  styleUrls: ['home.page.scss'],
})
export class HomePage {

  public press() {
    // tslint:disable-next-line:only-arrow-functions
    $.ajax({
      url: "demo_test.txt", // or any URL
      success: function(result) {
      $("#div1").html(result);
    }});
  }
}

我想在我的 Ionic 4 应用程序中从远程 HTML、PHP 或文本文件进行 AJAX 调用。

标签: androidajaxionic-frameworkangular7ionic4

解决方案


您应该使用 angular/http 中的 httpclient。这是非常直接的事情。

https://angular.io/guide/http

this.http.get(url).subscribe(data => {
  console.log(data)
}, err => {
  console.log(err)
}

推荐阅读