我正在使用表单并提交一个名字,姓氏,电子邮件,用户名作为字段的表单 . 当ajax返回时,它返回一串错误消息 . 如何分离错误消息?我正在使用此代码
$(document).ready(function(){$(":button[value=Done]").click(function(){data_first_name = $("#first_name").val(); data_last_name = $("#last_name").val(); data_user_name = $("#user_name") .val(); data_email = $("#email").val(); data_role = $("#role").val(); data_password = $("#password").val(); data_profile_image = $("#upload_admin_image").val(); $ .post("process/process_add_user.php",{first_name:data_first_name,last_name:data_last_name,email:data_email,role:data_role,password:data_password,user_name:data_user_name,profile_image:data_profile_image},function(resp){$("#first_name_error").html(resp); });});});
2 回答
要在网页上以文本形式输出AJAX响应,请先在HTML中创建一个新的
DIV
并使用以下脚本将Ajax文本响应写入
DIV
. 您需要使用函数调用它 .有关Ajax响应的更多信息http://www.w3schools.com/ajax/ajax_xmlhttprequest_response.asp
你可以使用response code . 代码应该代表实际的错误 .
您可以将错误消息集合作为JSON字符串发送,并在错误处理函数中根据需要处理这些错误 .
在出现错误的情况下:
在正常情况下:
当您可以为各个状态代码设置事件处理程序甚至只是
success
和fail
回调时,此方案适用于许多ajax库 .jQuery的一个例子:
使用vanilla JavaScript的示例: