我有下面的脚本。当我执行 ajax 函数时,它会在控制台中正确打印具有更新值的 photobombAvatar。但是,我在将照片炸弹头像更新为全局变量时遇到问题。
<script>
var photobombAvatar = "<?php echo $this->user_data['avatar']; ?>";
$(function () {
var uploader = new ss.SimpleUpload({
button: btn,
url: '<?php echo $this->form_action; ?>',
sessionProgressUrl: 'data/js/plugins/thisSCRIPT.php',
name: 'file',
debug: true,
data: {name: 'upload_avatar'},
responseType: 'json',
onSubmit: function (filename, ext) {
// do a bunch of stuff
},
onComplete: function(file, json) {
if (json.success) {
var pic = new Pic();
var photobombAvatar = json.success;
pic.src = json.success;
}
console.log(photobombAvatar);
}
});
});
//another js fxn that uses the updated global photobombAvatar.
</script>
我已经阅读了一堆使用回调函数的示例,但似乎无法使其工作。这是我尝试过的一个例子:
<script>
var photobombAvatar = "<?php echo $this->user_data['avatar']; ?>";
$(function photobomb(callback) {
var uploader = new ss.SimpleUpload({
button: btn,
url: '<?php echo $this->form_action; ?>',
sessionProgressUrl: 'data/js/plugins/thisSCRIPT.php',
name: 'file',
debug: true,
data: {name: 'upload_avatar'},
responseType: 'json',
onSubmit: function (filename, ext) {
// do a bunch of stuff
},
onComplete: function(file, json) {
if (json.success) {
var pic = new Pic();
var photobombAvatar = json.success;
pic.src = json.success;
callback(json);
}
console.log(photobombAvatar);
updatedVAR(photobombAvatar);
}
});
});
photobomb();
function updatedVAR(photobombAvatar) {
console.log(photobombAvatar);
// do the rest of the js I need with the updated photobombAvatar value.
}
</script>
提前感谢您的任何建议或想法。
您正在 onComplete 函数中定义photobombAvatar
new。在照片炸弹头像之前移除var
。喜欢:
onComplete: function(file, json) {
if (json.success) {
var pic = new Pic();
var photobombAvatar = json.success;
pic.src = json.success;
callback(json);
}
更改为:
onComplete: function(file, json) {
if (json.success) {
var pic = new Pic();
photobombAvatar = json.success;
pic.src = json.success;
callback(json);
}