同步获取以替换同步 ajax

synchronous fetch to replace synchronous ajax

我知道不建议进行同步调用,但我必须在 ajax 中进行同步调用,因为我正在处理一个需要用户操作才能获得响应的设备。到目前为止,我下面的 ajax() 代码一直运行良好。我收到 "Synchronous XMLHttpRequest on the main thread is deprecated" 的警告。 同步调用 returns 响应(来自设备),我将其传递给第二个调用并生成输出:

<script>  
var result = $.ajax({
    url: 'url',
 async: false,
 type: "GET"
}).responseText;

$(document).ready(function() {   
    $.post("https://example.com/doit.php", {
        response: result,account: <?php echo $account ?>,dn:<?php echo $dn ?>, mode:<?php echo $dest ?>},function(data){document.write(data);});
    });
</script>

如何更改同步调用以保持其正常工作。

试试这个

<script>
    $(document).ready(function(){   
        $.ajax({
          url: 'url',
          type: "GET",
          success: function(r){
             var result = r.responseText;
             $.post("https://example.com/doit.php",{response: result,account: <?php echo $account ?>,dn:<?php echo $dn ?>, mode:<?php echo $dest ?>},function(data){document.write(data);});
          }
        });
    });
</script>

在 get 的成功回调中执行 post 请求:

$.ajax({
    url: 'url',
    type: "GET"
})
    .then(function(responseText) {
        return $.post(...)
    })
    .then(function(postResponse) {
        ..do whatever
    })