如何使用AJAX提交dojo表单,如果有错误,在错误填写的字段附近打印错误? 现在我正在做类似的事情:
dojo.ready(function() {
var form = dojo.byId("user_profile_form");
dojo.connect(form, "onsubmit", function(event){
dojo.stopEvent(event);
var xhrArgs = {
form: form,
handleAs: "json",
load: function(responseText){
var result_data = zen.json.getResult(responseText);
dojo.byId("response").innerHTML = "Form posted.";
},
error: function(error){
// We'll 404 in the demo, but that's okay. We don't have a 'postIt' service on the
// docs server.
dojo.byId("response").innerHTML = "Form posted.";
}
}
// Call the asynchronous xhrPost
dojo.byId("response").innerHTML = "Form being sent..."
var deferred = dojo.xhrPost(xhrArgs);
});
但我不知道如何打印错误
答案 0 :(得分:1)
有几种方法可以做到这一点。我更喜欢的是订阅IO Pipeline Topics
如有错误,请订阅/dojo/io/error
主题。这是一个Growl错误的例子。
dojo.subscribe("/dojo/io/error", function(/*dojo.Deferred*/ dfd, /*Object*/ error){
// Triggered whenever an IO request has errored.
// It passes the error and the dojo.Deferred
// for the request with the topic.
var responseTextObject = dojo.fromJson(error.responseText)
var growlMessage = '';
if (responseTextObject && responseTextObject.message) {
growlMessage += responseTextObject.message
} else {
// Don't Growl the xhr cancelled messages.
if (error.message == 'xhr cancelled') {
return;
}
growlMessage = error.message
}
new ext.Growl({
message: growlMessage
});
});
服务器应提供响应中的所有错误详细信息。在此示例中,预期会出现JSON格式的响应,但如果未提供,则仍会显示错误。
如果您想查看好的无效字段样式,请将小部件放在dijit.form.Form
中