简体   繁体   English

使用同步In Ajax请求

[英]Using synchronous In Ajax Request

For some business I need use synchronous for Ajax, could someone help me how to use, I find some code like this but not understand what it do. 对于某些业务,我需要对Ajax使用同步,是否有人可以帮助我使用,我发现了一些类似这样的代码,但不了解它的作用。

function getData(productId, storeId) {
  var returnHtml = '';

  jQuery.ajax({
    url: "/includes/unit.jsp?" + params,
    async: false,
    cache: false,
    dataType: "html",
    success: function(html){
      returnHtml = html;
    }
  });

  return returnHtml;
}

Your code is the standard jQuery ajax code with async property set to false. 您的代码是标准jQuery ajax代码,其async属性设置为false。 This forces the ajax call to be synchronous. 这迫使ajax调用是同步的。

function getData(productId, storeId) {
  var returnHtml = '';

  jQuery.ajax({
    url: "/includes/unit.jsp?" + params,
    async: false, // <-- this forces the ajax call to be synchronous.
    cache: false,
    dataType: "html",
    success: function(html){ //<<-- This is where you get the ajax response
      returnHtml = html;
    }
  });

  return returnHtml;
}

Just be aware that synchronous ajax calls are not very performant. 请注意,同步ajax调用的性能不是很高。

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

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