以正确的顺序执行jQuery脚本

时间:2017-09-19 16:31:44

标签: javascript jquery

我有一个chrome扩展程序,它包含一个html页面和一些.js脚本。

在我的页面上,我有一个按钮,

HTML

<script src='api.js'></script>
<script src="jquery-git.js" type="text/javascript"></script>
<button name="Log" type="submit" id="Log-submit">Save Result</button>

api.js

//  Add document to Elastic Search (Log)
$(document).ready(function() {
  $('#Log-submit').on('click', function(e) {
    e.preventDefault();
    var btn = $(e.target);
        btn.attr("disabled", "disabled"); // disable button
    $.ajax({
      data: JSON.stringify({ name: $( "div.Name" ).text(), xentime: entime, incident: "INC-"+ $("#incID").val(), team: $( "div.Team" ).text(), response: $("#option_id option:selected").text(), timestamp: $.now(), xuser: $("div.xuser").text(), xlocation: $("div.xloc").text()}),  //use variable value/text in JSON POST request
      dataType: 'json',
      type: 'POST',
      contentType: "application/json; charset=utf-8",
      url: 'http://myapi.com:9200/escalations/escalation/'
    });
  });
});


// Close Window
 $(document).ready(function() {
  $("#Log-submit").click(function() {
   window.close();
  });
});

点击后,它会对我的弹性搜索索引进行API调用,并填充一些JSON。

然后我想让窗户关闭。

当我在没有“Close window”的情况下运行它时,我的数据填充但是窗口保持打开状态。

如果我使用“Close Window”运行,我的数据不会填充,但我的窗口会关闭。

如何才能以正确的顺序运行?

3 个答案:

答案 0 :(得分:5)

关闭窗口正在取消AJAX请求。在AJAX请求的成功函数中执行此操作。

$(document).ready(function() {
  $('#Log-submit').on('click', function(e) {
    e.preventDefault();
    var btn = $(e.target);
        btn.attr("disabled", "disabled"); // disable button
    $.ajax({
      data: JSON.stringify({ name: $( "div.Name" ).text(), xentime: entime, incident: "INC-"+ $("#incID").val(), team: $( "div.Team" ).text(), response: $("#option_id option:selected").text(), timestamp: $.now(), xuser: $("div.xuser").text(), xlocation: $("div.xloc").text()}),  //use variable value/text in JSON POST request
      dataType: 'json',
      type: 'POST',
      contentType: "application/json; charset=utf-8",
      url: 'http://myapi.com:9200/escalations/escalation/',
      success: function() {
        window.close();
      }
    });
  });
});

答案 1 :(得分:1)

你的.ajax()调用有一些你可以在配置对象中提供的回调函数,在其中一个函数中使用window.close()。

值得注意的是,你的情况是成功的&#34; (如@Barmar的回答中提到的),&#34;错误&#34;和&#34;完成&#34;

答案 2 :(得分:1)

很多时候,如果浏览器试图执行window.close();之类的关闭请求,浏览器将中止挂起的AJAX调用并阻止执行新的调用。因此,将$.ajax({ data: JSON.stringify({ name: $( "div.Name" ).text(), xentime: entime, incident: "INC-"+ $("#incID").val(), team: $( "div.Team" ).text(), response: $("#option_id option:selected").text(), timestamp: $.now(), xuser: $("div.xuser").text(), xlocation: $("div.xloc").text()}), //use variable value/text in JSON POST request dataType: 'json', type: 'POST', contentType: "application/json; charset=utf-8", url: 'http://myapi.com:9200/escalations/escalation/' }).always(function () { window.close(); }); 语句添加到AJAX调用的promise回调中,以允许它首先完成:

.always()

如果您希望window关闭AJAX调用是否成功,则可以使用window。同样,如果您希望仅在返回成功回复后才关闭.always(),则可以将.done()更改为from selenium import webdriver BaseUrl = "http://google.com" browser = webdriver.Firefox() browser.get(BaseUrl)

参考: .always().done()

相关问题