我一直在努力保存来自网络表单的数据并且无法获得返回的标准验证。认为这是某种异常问题,我只是没有得到。使用parse.com api保存对象是基于backbone.js构建的,因此它非常相似。出于某种原因,我可以将数据保存到我的数据库没有问题,但是当我尝试引入某种验证时,它会搞砸。寻找有关如何从服务器正确获取成功验证的一些信息。现在它每次都会出现错误,似乎会使服务器无法保存数据。
以下是在提交时执行的代码。我已经展示了我尝试保存数据的三种方法。
$("#f1").submit(function(event) {
var NewRes = Parse.Object.extend("Customer");
var newRes = new NewRes();
newRes.set("FirstName", "Ricky");
newRes.set("LastName", "Bobby");
//works every time, but I have no return validating it
newRes.save();
//saving with callback options, doesn't save anything to the database and hits error message
newRes.save(null, {
wait: true,
success: function(newRes, response) {
alert("success" + response.message);
},
error: function(newRes, response) {
alert("errorcode: " + response.code + " Message: " + response.message);
}
});
//saving with promises, doesn't save anything and hits error message
newRes.save().then(function(response) {
alert("success");
}, function(error) {
alert("error");
});
});
以下是错误消息的结果:
errorcode: 100 Message: XMLHttpRequest failed: {"statusText":"","status":0,"response":"","responseType":"","responseXML":null,"responseText":"","upload":{"ontimeout":null,"onprogress":null,"onloadstart":null,"onloadend":null,"onload":null,"onerror":null,"onabort":null},"withCredentials":false,"readyState":4,"timeout":0,"ontimeout":null,"onprogress":null,"onloadstart":null,"onloadend":null,"onload":null,"onerror":null,"onabort":null}
答案 0 :(得分:5)
没有这个工作的原因是因为form.submit()函数在任何parse.com异步函数完成之前完成。为了防止这种情况,我使用了preventdefault方法来停止提交表单。然后我使用location.reload()在我的parse.com请求成功完成或失败后刷新页面。
$("#f1").submit(function(event) {
event.preventDefault();
var NewRes = Parse.Object.extend("Customer");
var newRes = new NewRes();
newRes.set("FirstName", "Ricky");
newRes.set("LastName", "Bobby");
//saving with promises
newRes.save().then(function(response) {
alert("success");
location.reload(); //refreshes the form
}).catch(function(error) {
alert("error");
location.reload();
});
});