我遇到了JSON请求的问题。我有一个页面有2个不同的请求, 第一个按预期工作,但第二个不断出现意外的令牌o错误,我不明白。 第一个ajax电话:
$.ajax({
type:'POST',
dataType: 'json',
url: 'inc/getloads.php?q='+boxnum,
success:function(data){
if (data.l_id && data.u_id) {
var t_id = data.t_id;
var d_id = data.d_id;
var b_id = data.b_id;
var bcont = data.bcont;
var po_num = data.po_num;
var f_date = data.f_date;
var t_date = data.t_date;
var depart = data.depart;
var dest = data.dest;
var miles = data.miles;
var p_amt = data.p_amt;
var tpm = data.tpm;
var d_total = data.d_total;
}
这很好用,所有数据都是应该的,但这是第二次调用:
$.ajax({
type:'POST',
dataType: 'json',
url: 'inc/getfunctions.php?q='+boxnum+'&func=load_po',
success:function(data){
if (data.po_num) {
var e_id = data.l_id;
var load_num = data.load_num;
var expense = data.expense;
var ex_cost = data.ex_cost;
var po_num = data.po_num;
}
这一个用意想不到的令牌炸弹,我读过关于不解析它的两次,这是一个解决方案吗? 第二次电话的PHP:
$func = $_REQUEST['func'];
$_SESSION['func']= $func;
switch ($func) {
case 'load_po':
$q = intval($_REQUEST['q']);
require_once("medoo.min.php");
$database = new medoo();
$database->select("Expenses", "*", array(
"id" => $q));
if (count($datas)>0) {
foreach($datas as $data){
$l_id=$data['id'];
$po_num=$data['po_num'];
$load_num=$data['load_num'];
$expense=$data['expense'];
$ex_cost=$data['ex_cost'];
}
$rdata = array(
'l_id'=> $l_id,
'po_num'=> $po_num,
'load_num'=> $load_num,
'expense'=> $expense,
'ex_cost'=> $ex_cost
);
echo json_encode($rdata); //$datas['first_name']
} else {
echo 'no datas';
}
break;
}
答案 0 :(得分:0)
$ datas永远不会被初始化 并且你有一个可以多次执行的foreach,但你只能分配一次结果:
foreach($datas as $data){
$l_id=$data['id'];
$po_num=$data['po_num'];
$load_num=$data['load_num'];
$expense=$data['expense'];
$ex_cost=$data['ex_cost'];
}
$rdata = array(
'l_id'=> $l_id,
'po_num'=> $po_num,
'load_num'=> $load_num,
'expense'=> $expense,
'ex_cost'=> $ex_cost
);
所以如果您只想从数据中检索一行,请执行以下操作:
$data = $datas[0];
$l_id=$data['id'];
$po_num=$data['po_num'];
$load_num=$data['load_num'];
$expense=$data['expense'];
$ex_cost=$data['ex_cost'];
$rdata = array(
'l_id'=> $l_id,
'po_num'=> $po_num,
'load_num'=> $load_num,
'expense'=> $expense,
'ex_cost'=> $ex_cost
);