Ajax调用了Web服务器,但没有执行回调函数

ajax函数

function Verify(ccode,dgh)
{
    str = "ccode="+ccode+"&dgh="+dgh;
    console.log(str);//this outputs means that this functions gets called
    $.ajax({
        type: "POST",
        url: "ajax/verify",
        data: str,
        async: false,
        cache: false,
        error: function (xhr, ajaxOptions, thrownError)
        {
            console.log(xhr.status);
            console.log(thrownError);                         
        },
        success: function(json)
        {
            console.log("in-fun: "+json.code); //does not gets executed
            return json.code; //does not return value
        },
        failure:function(response)
        {
            console.log("Ajax call failed"); //does not executes
        }
    });
}

上面的ajax函数被称为var e = Verify(var1, var2); e的值在ajax请求后未定义。

ajax请求确实打到了我的web服务器,并在apache日志和开发工具中可见,并返回200 OK。 Ajax端点正在工作,并返回一个有效的json。 页面输出标题也设置为json

编辑:更新上面的代码

function Verify(ccode,dgh)
{
    var retData = '';
    str = "ccode="+ccode+"&dgh="+dgh;
    console.log(str); // this works
    $.ajax({
        type: "POST",
        url: "ajax/verify",
        data: str,
        async: false,
        cache: false,
        error: function (xhr, ajaxOptions, thrownError)
        {
            console.log(xhr.status); //does not gets called
            console.log(thrownError);

        },
        success: function(json)
        {
            console.log("in-fun: "+json.code); //this does not ouputs anything
            retData = json.code;
        },
        complete:function(response)
        {
            console.log("Complete called"); //does not gets called
        }
    });
    return retData;
}

由于jQuery AJAX调用已经被使用,你可以依靠它的延迟对象如下:

function Verify(ccode, dgh)
{
  var str = "ccode="+ccode+"&dgh="+dgh;
  console.log(str);  //debug outputs
  return $.ajax({
      type: "POST",
      url: "ajax/verify",
      data: str
   });
}

Verify(var1, var2).done(function(json) {
  if (json) {
    var e = json.code;
    // more code for the success case
  }
  else {
    console.log("Invalid server response"); 
  }
}).fail(function() {
  console.log("Ajax call failed"); 
});
链接地址: http://www.djcxy.com/p/55313.html

上一篇: Ajax calls hitting web server but no callback functions get executed

下一篇: Post Ajax function to return true or false