這是我的AJAX代碼來獲取下拉記錄。如何在AJAX成功函數中獲取json數據
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>");
});
}
}
});
}
該文件用於拉取記錄或獲取AJAX成功記錄。
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);
?>
你怎麼發送一個JSON頭與PHP? –
所以你有一些代碼。你有問題嗎? http://stackoverflow.com/help/how-to-ask(你有什麼問題?你是如何調試它的?你得到了什麼錯誤信息?你知道在哪裏尋找錯誤信息嗎?原始的HTTP請求和響應,你看起來像?等等) – Quentin
你解析了響應JSON http://api.jquery.com/jquery.parsejson/ – ravisachaniya