开发者社区> 问答> 正文

Json中的dataType:"json"问题

在如下代码中,只要设置dataType:"json", $ajax 中的success()就无法执行,直接被error:接管。这是为什么呢???(已设置所有charset=utf-8,DB的charset=gbk)

JS代码:

type: "POST",
url: "select_branch.php" ,
data: { "region":$("#bg_region").val(),"kind":init_kind },
dataType: "json",
complete :function(){ alert("The progream is complete!!!"); },
error: function() { alert("Jquery Ajax request error!!!"); },
success: function(data)
{
if (data)
{
var str = '';
for (var i=0, x=data.length; i<x; i++)
{
str += '<option value=' + data[i] + '>' + data[i] + '</option>';
}
$("#bg_branch").empty().append(str);
}
}
});
PHP代码:


<?php
 
include ('../../conn_xxjsj.php');
$init_region=$_POST['region'];
$init_kind=$_POST['kind'];
$init_sql="select branch from xxjsj."+$init_kind+" where region= '"+$init_region+"' ";
$init_query=@mysql_query($init_sql,$conn);
$res=array();
while($init_row=@mysql_fetch_array($init_query))
{
$res[] = iconv('gbk', 'utf-8', $init_row['branch']);
}
if (count($res))
{
echo json_encode($res);
} else
{
die;
}
@mysql_free_result($init_query);
@mysql_close($conn);

?>

展开
收起
a123456678 2016-07-11 17:40:45 3171 0
1 条回答
写回答
取消 提交回答
  • error: function (XMLHttpRequest, textStatus, errorThrown) {
        // 通常 textStatus 和 errorThrown 之中
        // 只有一个会包含信息
        this; // 调用本次AJAX请求时传递的options参数
    }
    2019-07-17 19:54:28
    赞同 展开评论 打赏
问答标签:
问答地址:
问答排行榜
最热
最新

相关电子书

更多
低代码开发师(初级)实战教程 立即下载
冬季实战营第三期:MySQL数据库进阶实战 立即下载
阿里巴巴DevOps 最佳实践手册 立即下载