嗨,朋友们,我遇到了一个可怕的错误,我正在使用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…:-)