jquery表单插件json不能正常工作
我正在使用jquery表单插件上传图像,一切都进行得很顺利。我遇到的问题是php脚本从服务器发送的json响应jquery表单插件json不能正常工作,jquery,jquery-forms-plugin,jqueryform,Jquery,Jquery Forms Plugin,Jqueryform,我正在使用jquery表单插件上传图像,一切都进行得很顺利。我遇到的问题是php脚本从服务器发送的json响应 //This is the way i output and encode the jason in my php script $errors['success'] = false; $errors['.imgContainer'] = 'Image exceeds maximum size.'; echo json_encode($errors); //Json respons
//This is the way i output and encode the jason in my php script
$errors['success'] = false;
$errors['.imgContainer'] = 'Image exceeds maximum size.';
echo json_encode($errors);
//Json response copied from console in FireFox 14.0.1
{"success":false,".imgContainer":"Image exceeds maximum size."}
//Console response for:
console.log(xhr.responseText["success"]);
Undefined
//This should have been true right?
我正在使用jQuery1.8.0
//My Jquery
$('.imageForm').live('click', function(e) {
if (!isValid) {
e.preventDefault();
}
else {
var bar = $('.bar');
var percent = $('.percent');
var response = $('.imgHolder');
var status = $('#status');
$('form').ajaxForm({
contentType : "application/json; charset = utf-8",
dataType: "json",
type : "POST",
url : "imgHandle.php",
cache : false,
//target: "#status",
beforeSend: function() {
status.empty();
var percentVal = '0%';
bar.width(percentVal)
percent.html(percentVal);
$(".imgContainer :input").attr("disabled", true);
},
uploadProgress: function(event, position, total, percentComplete) {
var percentVal = percentComplete + '%';
bar.width(percentVal)
percent.html(percentVal);
},
complete: function(xhr) {
console.log(xhr.responseText["success"]);
}
});
}
});
为什么我在尝试访问(xhr.responseText[“success”])时没有定义?我希望得到正确或错误以及.imgContainer的价值
也许它没有将其解释为json。如何解决此问题?您必须使用
success
而不是complete
。在complete
中,您只能访问原始JSON字符串,必须先解析它,然后才能将其作为对象访问
在success
中,参数将包含JSON对象(而不是字符串),因此您可以直接访问它
success: function(data) {
console.log(data.success);
}
谢谢你帮我做这件事。一切顺利,成功:而不是完成: