简体   繁体   English

如何在AJAX成功函数中获取json数据

[英]How to get json data in AJAX success function

This is my AJAX code to fetch dropdown record. 这是我的AJAX代码,用于提取下拉记录。

function item_value() {
  $.ajax({
    type: "POST",
    url: "ajax.php",
    contentType: "application/json; charset=utf-8",
    async: false,
    data: data,
    dataType: "json",
    success: function (json) {
      if (json == '') {
        alert('No Records');
      } else {
        alert(json);
        $.each(json, function (index, elem) {
          $("#item_select1").append("<option value=\"" + elem.value+ "\">" + elem.name+ "</option>");
        });
      }
    }
  });
}

This file is used to pull record or get get records in AJAX success. 该文件用于在AJAX成功时提取记录或获取记录。

Ajax.php Ajax.php

<?php
include('../dbconnection/comp_connect.php');
$qry_item="SELECT item_id,item_name,item_unit,rate,description,tax_id,status FROM item";
$view_item=$conn->prepare($qry_item);
$view_item->execute();
$rs_item=$view_item->fetch((PDO::FETCH_ASSOC))
$data = array('item_name' =>$rs_item['item_name'],
              'item_unit'=>$rs_item['item_unit'],
              'rate'=>$rs_item['rate'],
              'description'=>$rs_item['description'],
              'tax_id'=>$rs_item['tax_id']
             );
echo json_encode($data);
?>

It looks like the data that you're creating in PHP doesn't line up with what you're trying to do in your AJAX success function. 看起来您在PHP中创建的数据与您在AJAX成功函数中尝试执行的操作不一致。 The PHP code fetches one row from your database, so the JSON it will return will look like: PHP代码读取一行从数据库中,所以它会返回JSON的样子:

{
    "item_name": "something",
    "item_unit": "something",
    "rate": "something",
    "description": "something",
    "tax_id": "something"
}

But this part of your success function 但这是您成功功能的一部分

$.each(json, function (index, elem) {
  $("#item_select1").append("<option value=\"" + elem.value + "\">" + elem.name + "</option>");
});

needs an collection of objects ( elem ) with name and value properties. 需要具有namevalue属性的对象( elem )的集合。 Instead, with what you're getting from PHP, each elem will be a string or number, so elem.value and elem.name will be undefined. 取而代之的是,从PHP中获得的东西,每个elem将是一个字符串或数字,因此elem.valueelem.name将是未定义的。

Since you're using this to append options to a select, your PHP needs to return multiple rows, and the keys you assign in PHP need to match the properties you refer to in your success function. 由于使用此选项将选项附加到一个选择上,因此您的PHP需要返回多行,并且您在PHP中分配的键需要与您在成功函数中引用的属性匹配。 Something like this should be closer to what you want. 这样的事情应该更接近您想要的。

<?php
include('../dbconnection/comp_connect.php');
$qry_item = "SELECT item_id, item_name FROM item";
$view_item = $conn->prepare($qry_item);
$view_item->execute();
while ($item = $view_item->fetch((PDO::FETCH_ASSOC)) {
    $data[] = ['value' => $rs_item['item_id'], 'name' => $rs_item['item_name']];
}
echo json_encode($data);

In you php script make sure to add following headers before you print our the encoded data 在您的php脚本中,请确保在打印编码后的数据之前添加以下标头

header('Content-type: application/json');
echo json_encode($data);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM