[英]Json response error JSON.parse: unexpected end of data error
嗨,朋友们,我遇到了一个奇怪的错误,我正在使用ajax从正在使用php查询的其他文件中获取数据,它返回json_encode但json.parse存在问题...这是我的代码....
$.get("pgs/dpg.php",{pg:"18", type:"rec", obs_code:$("#obs_code").val(),lib_code:$("#lib_code").val()},function(data){
obj = JSON.parse(data);
$("#obs_focused").val(obj.obs_focused);
$("#obs_projects").val(obj.obs_projects);
});
这是我的PHP文件.....
switch($_REQUEST['type']){
case 'rec' :
$sql=mysql_query("select obs_attendance_punctuality, obs_general_fitness, obs_listening,
obs_independence, obs_special_mention, obs_participation,
obs_responsibility, obs_awards, obs_nobooks, obs_focused, obs_care_env, obs_projects,
obs_hand_control, obs_home_read, obs_workshop_parents, obs_workshop_child,
obs_field_trips, obs_homework, obs_eating, obs_batroom, obs_relation, obs_themes,
obs_courtesy, obs_instructions, obs_initiative, obs_alertness, obs_descipline, obs_assembly,
obs_interest, obs_grade, obs_undergarments, obs_general_hygiene, ".$xtflds."
obs_clarity, obs_clarity_rem, obs_tone_pitch, obs_tone_pitch_rem, obs_confidence,
obs_confidence_rem, obs_hand_coordination, obs_hand_coordination_rem, obs_eye_coordination,
obs_eye_coordination_rem, obs_body_language, obs_body_language_rem, obs_enthusiasm_interest,
obs_enthusiasm_interest_rem, obs_facial_expressions, obs_facial_expressions_rem,
obs_content, obs_content_rem, obs_vocabulary, obs_vocabulary_rem, obs_body_posture,
obs_body_posture_rem, remarks
from obsr
where obs_code =".$_REQUEST['obs_code']);
$data=mysql_fetch_array($sql);
echo json_encode($data);
return;
break;
}
请帮我解决这个问题.........
console.log(数据)
正在输出...
Uncaught SyntaxError: Unexpected end of input
console.log(data)
ReferenceError: data is not defined
message: "data is not defined"
stack: (...)
get stack: function () { [native code] }
set stack: function () { [native code] }
__proto__: Error
它不起作用........
在JavaScript中,使用$.getJSON()
代替$.get()
...
在PHP中,请务必使用
header('Content-type: application/json');
如果您已经使用它,并且您的php文件之前没有输出任何其他内容
echo json_encode($data);
,即使$ data恰好为空,它也必须工作... :-)
您必须使用mysql_fetch_assoc而不是mysql_fetch_array。 因为mysql_fetch_array仅返回值数组而不是键值对。
并确保在回显输出后没有打印的语句。 因此,您必须使用exit()或die()而不是“ return”语句。
并确保您的php代码正确打印编码的json。 您可以通过Firebug或Chrome开发者工具进行检查。
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.