Ajax calls hitting web server but no callback functions get executed

The ajax function

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
        }
    });
}

the above ajax function is called as var e = Verify(var1, var2); the value of e is undefined after the ajax request.

The ajax request does hit my web server and visible in apache logs and dev tools and returns 200 OK. Ajax endpoint is working and does returns a valid json. The page output header is also set to json

EDIT: updated the above code

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/55314.html

上一篇: 添加到<select>的选项在iPhone上的列表框中不可见

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