Javascript 从php获取json错误
我有一个问题:我试图从php json中获取一个值,但得到了一个错误。这是我的语法:Javascript 从php获取json错误,javascript,php,json,Javascript,Php,Json,我有一个问题:我试图从php json中获取一个值,但得到了一个错误。这是我的语法: <select name='posting_daerah' id='optionDaerah' class='form'> <?php $dataDaerah=mysql_query("SELECT*FROM t_daerah"); while($d=mysql_fetch_array($dataDaerah)){ echo"&l
<select name='posting_daerah' id='optionDaerah' class='form'>
<?php
$dataDaerah=mysql_query("SELECT*FROM t_daerah");
while($d=mysql_fetch_array($dataDaerah)){
echo"<option value='$d[daerah_id]'>$d[daerah_nama]</option>";
}
?>
</select>
<script type="text/javascript">
$(document).ready(function(){
$('#optionDaerah').change(function(){
var daerah_value=$(this).val();
alert(daerah_value);
var data ="{daerah:"+daerah_value+"}";
$.ajax({
type : "post",
url : "get_lokasi",
data : data,
dataType: "json",
success: function (data) {
$.each(function(index){
alert(index);
});
},
error: function(xhr,textStatus,err) {
console.log("readyState: " + xhr.readyState);
console.log("responseText: "+ xhr.responseText);
console.log("status: " + xhr.status);
console.log("text status: " + textStatus);
console.log("error: " + err);
}
});
});
});
</script>
我从控制台收到一个错误:
readyState: 4
status: 200 (index):125
text status: parsererror (index):126
error: SyntaxError: Unexpected token <
readyState:4
现状:200(索引):125
文本状态:parsererror(索引):126
错误:语法错误:意外标记<
如何修复此错误?您应该将您的SQL语句(如
SELECT*FROM
)与此SELECT*FROM
^分开,请告诉我。我会在同一行前面加一个空格。我想那不是错误的代码是的,不是。您应该查看开发人员工具上的“网络”选项卡,以查看服务器返回的内容。jquery会认为这是json,但这是无效的。我希望我说的很清楚。你需要仔细阅读,这样你就不会像这里那样造成更严重的后果。另外,mysql\u query
不应在新应用程序中使用。这是一个不推荐使用的接口,将从PHP的未来版本中删除。现代的替代方法与and类似,是一种更安全的组合查询的方法$\u POST
数据从不直接进入查询。
readyState: 4
status: 200 (index):125
text status: parsererror (index):126
error: SyntaxError: Unexpected token <