获取php json数据到jquery

我正在 php 和 jquery 项目中工作,我使用 ajax 向服务器端发出请求


现在我的 php 文件的 ajax 响应有问题


我有以下 php 代码


<?php


require 'db.php';

$code = $_POST['code'];


$status = 0;

$count = 0;

$name = "";

$guarantors = 0;

 

$sql = "SELECT * FROM clients WHERE code='$code'";

$result = mysqli_query($link, $sql);

if(mysqli_num_rows($result) > 0){

    $row = mysqli_fetch_array($result);

    $name = $row['name'];

    $status = 1;    

    $sqli = "SELECT * FROM guarantors WHERE client_code='$code'";

    $resulti = mysqli_query($link, $sqli);

    if(mysqli_num_rows($resulti) > 0){

        $guarantors = '[';

        while($rowi = mysqli_fetch_array($resulti)){

        $count ++;

            $guarantors .= '{id:'.$rowi['id'].', name:'.$rowi['guarantor_name'].'},';

        }

        $guarantors .= ']';

    }else{

        $guarantors = "0";

    }

}else{

    $status = 0;

    $returnText = "إسم المستخدم تم إستخدامه من قبل !";

}


echo json_encode(array("status"=>$status,"name"=>$name,"guarantors"=>$guarantors));

?>

我有这个 jquery 代码


  $.ajax({

    url:"read_client.php",

    method:"POST",

    data: {code:$('.code').val()},

    dataType: 'json',

    success:function(response){

      if(response.status == 1){

var result = $.parseJSON(response);

console.log(result.guarantors.name)


        YAFloader.close()

      }else{

        Swal.fire(

          'error'

        )

        YAFloader.close()

      }

    }

  });

现在我想获取每个id和namefrom guarantors,但我做不到


我需要帮助,请快点,有人可以帮助我吗???


谢谢你们


侃侃尔雅
浏览 87回答 1
1回答

陪伴而非守候

$guarantors应该是一个数组:$guarantors = [];if (mysqli_num_rows($resulti) > 0) {&nbsp; &nbsp; while ($rowi = mysqli_fetch_array($resulti)) {&nbsp; &nbsp; &nbsp; &nbsp; $count++;&nbsp; &nbsp; &nbsp; &nbsp; $guarantors[] = [&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'id' => $rowi['id'],&nbsp; &nbsp; &nbsp; &nbsp; &nbsp; &nbsp; 'name' => $rowi['guarantor_name'],&nbsp; &nbsp; &nbsp; &nbsp; ];&nbsp; &nbsp; }}&nbsp;然后在js端:result.guarantors.map(guarantor => {&nbsp; console.log(guarantor.id, guarantor.name);})
打开App,查看更多内容
随时随地看视频慕课网APP