【问题标题】:Cannot rewrite some html content with ajax无法用 ajax 重写一些 html 内容
【发布时间】:2019-11-26 06:19:57
【问题描述】:

我想用ajax 重写我的HTML 内容。一部分可以重写,但不能为其他部分重写。这是我的js:

$.ajax({
    url: 'http://localhost/ThePrpuz/public/mahasiswa/getDetail',
    data: {
        nim: nim
    },
    method: 'post',
    dataType: 'json',
    success: function (data) {
        console.log(data);
        if ((data.gender) == 'Wanita') {
            $('#foto').attr('src', 'http://localhost/theprpuz/public/img/cewek.jpg');
            $('#foto').attr('alt', 'Cewek');

        } else {
            $('#foto').attr('src', 'http://localhost/theprpuz/public/img/cowok.jpg');
            $('#foto').attr('alt', 'Cowok');
        }
        $('#nama').html(data.nama);
        $('#nim').html(data.nim);
        $('#univ').html(data.univ);
        $('#fakultas').html(data.fakultas);
        $('#jurusan').html(data.jurusan);
        $('#penjurusan').html(data.penjurusan);
        $('#ttl').html(data.tempat.concat(', ', data.tanggal));
    }
});

问题是,带有**ID: ttl**的内容可以重写。但没有为别人。结果像 this :

Nama :
Nim :
TTl : Somewhere, sometime
Univ : 
Fakultas :
Jurusan : 
Penjurusan :

在这种情况下,我使用 console.log(data) 来查看我的 ajax 是否有效。在console 中,显示:

{
  "nama": "Muhammad Fachri Saragih",
  "tanggal": "2001-09-11",
  "tempat": "Sibolga",
  "gender": "Pria",
  "univ": "Sriwijaya",
  "fakultas": "Ilmu Komputer",
  "jurusan": "Sistem Komputer",
  "penjurusan": "Jaringan",
  "nim": "9011281924069"
}

【问题讨论】:

    标签: javascript php html json ajax


    【解决方案1】:

    concat() 方法用于连接两个或多个数组,而不是字符串! 替换

    $('#ttl').html(data.tempat.concat(', ', data.tanggal));
    

    与

    $('#ttl').html(data.tempat + ', ' + data.tanggal);
    

    【讨论】:

      【解决方案2】:

      首先,尝试检查“数据”的数据类型。我认为它无法访问,因为数据类型仍然是字符串,而不是 JSON。 尝试将数据解析为 JSON。

      $.ajax({
      url: 'http://localhost/ThePrpuz/public/mahasiswa/getDetail',
      data: {
          nim: nim
      },
      method: 'post',
      dataType: 'json',
      success: function (data) {
          console.log(data);
          console.log(typeof data);
          var obj = JSON.parse(data);
          if ((obj.gender) == 'Wanita') {
              $('#foto').attr('src', 'http://localhost/theprpuz/public/img/cewek.jpg');
              $('#foto').attr('alt', 'Cewek');
      
          } else {
              $('#foto').attr('src', 'http://localhost/theprpuz/public/img/cowok.jpg');
              $('#foto').attr('alt', 'Cowok');
          }
          $('#nama').html(obj.nama);
          $('#nim').html(obj.nim);
          $('#univ').html(obj.univ);
          $('#fakultas').html(obj.fakultas);
          $('#jurusan').html(obj.jurusan);
          $('#penjurusan').html(obj.penjurusan);
          $('#ttl').html(obj.tempat.concat(', ', obj.tanggal));
      }
      });
      

      【讨论】:

      • 谢谢兄弟。这是正确的。数据是字符串,不是 JSON
      猜你喜欢
      • 2012-08-09
      • 1970-01-01
      • 2015-06-29
      • 1970-01-01
      • 1970-01-01
      • 2019-05-08
      • 1970-01-01
      • 2014-09-17
      • 1970-01-01
      相关资源
      最近更新 更多