承诺和上传数据
Promises and uploading data
我在在线发送表单或将其保存在本地时遇到了一些问题。我有一个Phonegap应用程序,它只是一个简单的形式,可以保存到数据库中,或者如果失败,则在本地保存。
我只设置了这个功能。首先,我想,我只是用布尔人做,运行功能,如果一切顺利,则返回 True。但这不起作用,因为代码只是在我有一个值要比较之前执行,这意味着该值将始终为 false。
这是现在的代码:
if(uploadDataAndImage(formData))
{
//Its true, do my things
}
else
{
//Its false
}
这是布尔函数:
function uploadDataAndImage(dataForm) {
var localURL = dataForm.cardurl;
console.log(localURL);
if(localURL == "")
{
$$.ajax({
type: "POST",
url: "upload.php",
crossDomain: true,
data: dataForm,
success:function(data)
{
console.log("Upload to server without card");
return true;
},
error: function()
{
console.log("Form couldn't upload to the server");
//alert("Couldnt upload to server, saving locally");
return false;
}
});
}
else{
//set upload options
var options = new FileUploadOptions();
options.chunkedMode = false;
console.log("Start upload picture");
var ft = new FileTransfer();
ft.upload(deLokaleURL, encodeURI("uploaderino.php"), win, fail, options);
function win(r) {
console.log("Code = " + r.responseCode);
console.log("R" + r.response);
console.log("Sent = " + r.bytesSent);
dataForm.cardurl = r.response;
var dataString = JSON.stringify(dataForm);
$$.ajax({
type: "POST",
url: "upload.php",
crossDomain: true,
data: dataForm,
success:function(data)
{
console.log("Upload to server with card");
return true;
},
error: function()
{
console.log("Form couldn't upload to the server, do nothing");
return false;
}
});
}
function fail(error) {
//alert("An error has occurred: Code = " + error.code);
console.log("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
showMessage("Image upload has failed, get a better internet connection and try again!" , function(){console.log("Upload van de form is mislukt!")});
return false;
}
}
}
但这显然是行不通的。现在我做了一些研究,问了一些问题,我找到了承诺。但我似乎无法理解如何使用它。
我是这样想的:
var deferred = $.Deferred();
deferred.resolve(uploadDataAndImage(formData));
deferred.done(function(value) {
//Upload went well so do my things
});
但这不是它的工作原理,对吧/?
在 uploadDataAndImage 中使用 deferred 返回一个承诺。然后,您可以使用 then() 和 catch() 在 ajax 调用完成时执行某些操作。
function uploadDataAndImage() {
var deferred = $.Deferred();
$$.ajax({
type: "POST",
url: "upload.php",
crossDomain: true,
data: dataForm,
success:function(data) {
deferred.resolve();
}, error: function(e) {
deferred.reject(e);
}
});
return deferred.promise;
}
uploadDataAndImage(formData).then(function() {
//Its true, do my things
}).catch(function() {
//Its false
});
我只是将其设置为使用回调函数。也许是这样的东西
uploadDataAndImage(formData, function(data){
console.log(data);
if(data){
//do something in here that needs doing as the ajax call was true
}
else
{
//Do something after the ajax returned an error...
}
})
function uploadDataAndImage(dataForm, callBack) {
var localURL = dataForm.cardurl;
console.log(localURL);
if(localURL == "")
{
$$.ajax({
type: "POST",
url: "upload.php",
crossDomain: true,
data: dataForm,
success:function(data)
{
console.log("Upload to server without card");
callBack(true);
},
error: function()
{
callBack(false);
}
});
}
else{
//set upload options
var options = new FileUploadOptions();
options.chunkedMode = false;
console.log("Start upload picture");
var ft = new FileTransfer();
ft.upload(deLokaleURL, encodeURI("uploaderino.php"), win, fail, options);
function win(r) {
console.log("Code = " + r.responseCode);
console.log("R" + r.response);
console.log("Sent = " + r.bytesSent);
dataForm.cardurl = r.response;
var dataString = JSON.stringify(dataForm);
$$.ajax({
type: "POST",
url: "upload.php",
crossDomain: true,
data: dataForm,
success:function(data)
{
callBack(true);
},
error: function()
{
callBack(false);
}
});
}
function fail(error) {
//alert("An error has occurred: Code = " + error.code);
console.log("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
showMessage("Image upload has failed, get a better internet connection and try again!" , function(){console.log("Upload van de form is mislukt!")});
return false;
}
}
}
你可以玩弄你想要函数做什么,我不是 100% 确定你想要什么:)但是,无论您放入函数中的内容,都将在进行 ajax 调用后执行。
你可以使用promises,像这样:
uploadDataAndImage(formData)
.done(function(){
// Its true, do my things
}).fail(function(){
//Its false
});
function uploadDataAndImage(dataForm) {
var deferred = $.Deferred();
var localURL = dataForm.cardurl;
console.log(localURL);
if (localURL == "") {
$$.ajax({
type: "POST",
url: "upload.php",
crossDomain: true,
data: dataForm,
success: function(data) {
console.log("Upload to server without card");
deferred.resolve();
},
error: function() {
console.log("Form couldn't upload to the server");
//alert("Couldnt upload to server, saving locally");
deferred.reject();
}
});
} else {
//set upload options
var options = new FileUploadOptions();
options.chunkedMode = false;
console.log("Start upload picture");
var ft = new FileTransfer();
ft.upload(deLokaleURL, encodeURI("uploaderino.php"), win, fail, options);
function win(r) {
console.log("Code = " + r.responseCode);
console.log("R" + r.response);
console.log("Sent = " + r.bytesSent);
dataForm.cardurl = r.response;
var dataString = JSON.stringify(dataForm);
$$.ajax({
type: "POST",
url: "upload.php",
crossDomain: true,
data: dataForm,
success: function(data) {
console.log("Upload to server with card");
deferred.resolve();
},
error: function() {
console.log("Form couldn't upload to the server, do nothing");
deferred.reject();
}
});
}
function fail(error) {
//alert("An error has occurred: Code = " + error.code);
console.log("An error has occurred: Code = " + error.code);
console.log("upload error source " + error.source);
console.log("upload error target " + error.target);
showMessage("Image upload has failed, get a better internet connection and try again!", function() {
console.log("Upload van de form is mislukt!")
});
deferred.reject();
}
}
return deferred.promise();
}
相关文章:
- $http中的Angular 1数据绑定承诺不起作用
- 如何在状态输入ui路由器时立即显示模板,然后当承诺被解决时显示数据
- 承诺和上传数据
- 访问 angularjs 工厂数据之外的承诺
- AngularFire $createUser 的承诺不返回包含用户数据的对象
- 在承诺函数从 pouchdb 填充数据之前触发 Ionic/Angular $watch的问题
- 如何在 ajax 请求中使用承诺对强制数据
- 在AngularUI路由器中访问多个承诺数据
- Angular JS承诺,JSON数据和有趣的时间
- 这是一种常见的模式吗?(返回数据而不是返回承诺)
- 通过承诺$q angularjs接收不完整的数据
- 从 Parse 承诺链返回数据
- 对没有返回数据的函数的承诺
- 角度“this”符号和来自服务的数据,带有承诺
- 蓝鸟承诺不会收到任何数据
- 角度承诺:如果可用,从缓冲区获取数据
- 刷新从承诺返回$http数据
- 在解决承诺 AngularJS/ES6 后检索存储在服务中的数据
- Angular 没有从 FS.readFile 获取带有承诺的数据
- 从承诺外部访问数据