1

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;
}
21
  • failure: -> error: Commented Jul 10, 2017 at 19:39
  • This function does not return anything, so the assignment uses undefined value Commented Jul 10, 2017 at 19:40
  • async: false, -> (blank) Commented Jul 10, 2017 at 19:41
  • @MaxZoom Yes and that's bothering me Commented Jul 10, 2017 at 19:42
  • 1
    failure: doesn't exist in $.ajax() as far as I am aware. The console.log might not be working because you don't get an expected reply (that's why the error: function gets console.logged). Check if the response in your console is what you were expecting. I have a feeling this might have something to do with dataType. That is just a guess however. Documentation: api.jquery.com/jquery.ajax Commented Jul 10, 2017 at 20:15

1 Answer 1

2

As the jQuery AJAX call is already used, you can rely on its deferred object as below:

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"); 
});
Sign up to request clarification or add additional context in comments.

1 Comment

Please notice that done() and fail() calls are outside the Verify() function, and use jqXHR object features. I agree that e has no true meaning except it is used in the success case.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.