嗨,朋友们,我有一个奇怪的错误,我正在使用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它不工作.............
发布于 2013-12-12 00:37:05
在JavaScript中,使用$.getJSON()而不是$.get()...
在PHP中,请确保使用
header('Content-type: application/json');如果您已经在使用它,并且您的php文件以前不输出任何其他内容
echo json_encode($data);,它必须工作,即使$data碰巧是null...:-)
发布于 2013-12-12 00:38:45
您必须使用mysql_fetch_assoc而不是mysql_fetch_array。因为mysql_fetch_array只是返回值的数组,而不是键-值对。
并确保在回显输出后没有打印的语句。因此,您必须使用exit()或die()而不是"return“语句。
并确保您的php代码正确打印编码的json。你可以查看这个,firebug或者chrome开发者工具。
https://stackoverflow.com/questions/20524508
复制相似问题