且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

在函数内执行jquery ajax调用时出现问题

更新时间:2022-05-03 10:01:46

可以通过两种方式对此进行标记.一种是使用成功回调:

There are two ways to taggle this. one is to use the success callback:

$.ajax({
   type: "GET",
   url: 'someURL',
   success: function(response) {
     AppendResponse(response);
  });

另一种方法是将async设置为false http://api.jquery.com/jQuery.ajax/:

the other is to set async to false http://api.jquery.com/jQuery.ajax/:

var a;
getAjax();
$('body').append('<div>'+a+'</div>');
function getAjax() {
  $.ajax({
   type: "GET",
   url: 'someURL',
   async: false,
   success: function(response) {
     a = response;
  });
}

关于非异步的重要说明:

Important note on non async:

跨域请求和dataType:"jsonp"请求不支持同步操作.

Cross-domain requests and dataType: "jsonp" requests do not support synchronous operation.