简体   繁体   English

无法在jquery ajax中实现延迟的Promise

[英]Can't implement deferred promise in jquery ajax

I'm struggling to implement deferred to get async ajax response. 我正在努力实现推迟以获得异步ajax响应。 I have this setup: 我有这个设置:

report.js report.js

function getReport(ref)
{
   $.ajax({
        url: "report.php", 
        dataType: 'json',
        data: { 
            ref: ref,
        },
        success: function(result){
           return (result);
           }
         }
    });
}

index.html 的index.html

<script>
function firstFunction() {
console.log ("start");
getReport(2, 'queue', 'hour', '2018-09-09', '2018-09-10', 'pageviews', 'page', 's1390_5bbb4639ff37');
};
var test = firstFunction();
alert(test);
</script>

Currently I get "undefined" returned straight away because the alert box doesn't wait for the ajax function to run. 当前,由于警报框不等待ajax函数运行,我立即返回“ undefined”。 Using some tutorials online i've tried to implement it this way: 使用一些在线教程,我试图通过这种方式实现它:

report.js report.js

function getReport(ref)
{
   var deferred = $.Deferred();
   $.ajax({
        url: "report.php", 
        dataType: 'json',
        data: { 
            ref: ref,
        },
        success: function(result){
           deferred.resolve(result);
           }
         }
    });
  returned deferred.promise();
}

index.html 的index.html

    <script>
    function firstFunction() {
    console.log ("start");
    getReport(2, 'queue', 'hour', '2018-09-09', '2018-09-10', 'pageviews', 'page', 's1390_5bbb4639ff37');
    };
$.when(getData()).done(function(value) {
    alert(value);
});

getData().then(function(value) {
    alert(value);
});
</script>

I've obviously made a few mistakes on the way because I'm getting the errors below and I can't seem to get past it: 我在途中显然犯了一些错误,因为我在下面发现了错误,但似乎无法克服:

Uncaught SyntaxError: Unexpected identifier
index2.html:12 start
index2.html:13 Uncaught ReferenceError: getReport is not defined
    at firstFunction (index2.html:13)
    at index2.html:16

I think the addition of the deferred object in getReport is unnecessary because $.ajax already creates one for you. 我认为没有必要在getReport中添加deferred对象,因为$.ajax已经为您创建了一个。 It might be better to modify your original code this way: 最好用这种方式修改原始代码:

function getReport(ref)
{
   return $.ajax({ // Return the deferred object here. It is returned by .ajax()
        url: "report.php", 
        dataType: 'json',
        data: { 
            ref: ref,
        } // Remove the callback here. 
          // You cannot call return in the way you're trying here. 
          // The context is different and outside of normal flow.
    });
}

then in your index file: 然后在您的索引文件中:

<script>
function firstFunction() {
  console.log ("start");
  // You must return the returned value here as well, otherwise variable `test` is going to be undefined.
  return getReport(2, 'queue', 'hour', '2018-09-09', '2018-09-10', 'pageviews', 'page', 's1390_5bbb4639ff37');
};
var test = firstFunction(); // test is now a deferred object
test.done(function(data) {
  alert(data);
});
</script>

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM