Json响应错误Json.parse:意外的数据结束错误
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
它不起作用。。。。。。。。。。。。。
您必须使用mysql_fetch_assoc而不是mysql_fetch _array。因为mysql_fetch_array只是返回值的数组,而不是键值对。
并确保在回显输出后没有打印的语句。因此,您必须使用exit()或die()来代替"return"语句。
并确保您的php代码正确打印编码的json。你可以检查这个,firebug或chrome开发工具。
在JavaScript中,使用$.getJSON()
而不是$.get()
。。。
在PHP中,请务必使用
header('Content-type: application/json');
如果您已经使用了它,并且您的php文件在之前没有输出任何其他内容
echo json_encode($data);
,它有工作,即使$data恰好为null…:-)
相关文章:
- ajax和json错误.php帖子错误
- json错误消息,可以't请参阅SyntaxError
- json错误json.parse:意外的数据结束错误
- 如何处理JSON错误消息
- D3.js 制作树时的 json 错误
- 评估 json 错误
- 使用 Web API MS CRM 时的 JSON 错误
- Javascript/JSON 错误:格式不正确
- JSON 错误:无法读取未定义的属性“xxx”
- 如何序列化全日历源.将循环结构转换为 JSON 错误
- jquery json错误,当我想从Steam市场获取JSON时
- Facebook javascript JSON 错误消息
- 控制台中的JSON错误:意外的令牌:
- 如何摆脱Firefox中的JSON错误
- 使用jQuery$.each,Json错误,结果为1
- 文件不存在错误,package.json错误
- JSON错误:应为..得到'未定义'
- 节点js中的JSON错误未定义为发布数据的前缀
- php后端出现JSON错误
- Jquery Json错误,来自steam的Json,访问控制允许来源