0

我不知道是否低于有效问题?或者只是我的愚蠢。

 function IsSlaExists(department) {
     var flag = "";
     $.ajax({
         type: "POST",
         data: "Type=ISSLAEXISTS&Department=" + encodeURI(escape(department)),
         url: "class-accessor.php",
         success: function (data) {
             //flag=data;  
             flag = "YES";
         }
     });
     return flag;
 }

 alert(IsSlaExists('department'));

我正在尝试返回的值,flag但即使我手动设置了标志的值,函数也会返回空白。我做错了什么?

4

1 回答 1

0
$.ajax({
       type: "POST",
       data: "Type=ISSLAEXISTS&Department=" + encodeURI(escape(department)),
       url: "class-accessor.php"
}).done(function(r){
      alert("flag");
});

这将在 AJAX 请求成功时向您发出警报。r将包含响应为您提供的数据。例如,您现在可以在 done 函数中调用另一个函数来处理请求。

由于 $.ajax 返回一个promise,您可以只返回 ajax 调用,然后将函数链接在一起

function ajaxCall(department) {
    return  $.ajax({
           type: "POST",
           data: "Type=ISSLAEXISTS&Department=" + encodeURI(escape(department)),
           url: "class-accessor.php"
    });
}

ajaxCall("myDept").done(function(response){
   alert(response);
})
于 2013-05-08T13:12:11.540 回答