简体   繁体   中英

Unable to get the response from an ajax call in the success function

After refering this stack link Ajax response not calling success:function() when using jQuery 1.8.3 am wondering why if I uncomment the datatype line the success function is not invoked.I can understand that dataType =JSON is not calling success function.Could some one help me out ?

function doAjaxCall(methodType,Url,params,requestType,callBackFunction)
{
    if(validate.variable(Url) && validate.variable(params))
    {
        $.ajax({
             type       :   methodType,
             url        :   Url,
//           dataType   :   'JSON', //if i uncomment i am not getting the response under     success
             data       :   params,
             success    :   function(data)
             {
                console.log(data);
                    callBackFunction(data);
             }
     });
}
}

function callBackFunctiontest(data)
{
    console.log('callBackFunctiontest : ' +data);

 }
doAjaxCall('GET','/getData?accountNumber=9840100672&oid=d11c9f66-4c55-4855-a99e-580ba8ef8d61','noparams','application/JSON',callBackFunctiontest);

If it's not passing in success function, put an error function. This way you'll be able to see the error and understand what's going on.

You can also open the developer console and have a look at the 'network' panel.

Assuming you're calling callBackFunctiontest which should then be calling doAjaxCall .

You're trying to use data when there is no variable named data in scope. It will throw an undefined exception and doAjaxCall will not get executed. So your AJAX request will never get sent.

Try getting rid of the console.log('callBackFunctionTest : ' +data); , or pass it a value for data .

I am using Express JS web framework and NodeJS, both latest versions - 4.xx

Issue #1 - Not all of my POST ajax calls were being sent to my NodeJS server code. How I found this is by trying to POST using Postman. All POSTMAN call requests were received by the server code but not the Express client requests. I checked by printing the console.log server debug statements to compare the findings between POSTMAN & Express Client.

Issue #2 - I was never receiving any response message from the server even though the server responded with a status 200 OK. For both the issues, I made a few changes, which I believe will help others.

  1. In the ajax call, I added e.preventDefault(); where 'e' is the event which is triggered via #click/#onclick.
  2. Within ajax function call, I added the parameter dataType: 'json'.
  3. In the NodeJS server code, index.js, I replaced res.send(req.body.search_data); with res.json(req.body.search_data); PS - e.preventDefault() was actually built for a different purpose but it helped because it prevents a link from following the URL which is what solved the above issues.

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM