简体   繁体   English

无法从成功函数中的 ajax 调用获得响应

[英]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 ? 引用此堆栈链接Ajax response not call success:function() when using jQuery 1.8.3我想知道为什么如果我取消注释数据类型行,成功函数不会被调用。我可以理解 dataType =JSON 没有调用成功函数。可以有人帮我吗?

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 .假设您正在调用callBackFunctiontest ,然后应该调用doAjaxCall

You're trying to use data when there is no variable named data in scope.当范围内没有名为data变量时,您正在尝试使用data It will throw an undefined exception and doAjaxCall will not get executed.它会抛出一个未定义的异常并且doAjaxCall不会被执行。 So your AJAX request will never get sent.所以你的 AJAX 请求永远不会被发送。

Try getting rid of the console.log('callBackFunctionTest : ' +data);尝试摆脱console.log('callBackFunctionTest : ' +data); , or pass it a value for data . ,或传递给它一个data值。

I am using Express JS web framework and NodeJS, both latest versions - 4.xx我正在使用 Express JS Web 框架和 NodeJS,两个都是最新版本 - 4.xx

Issue #1 - Not all of my POST ajax calls were being sent to my NodeJS server code.问题 #1 - 并非我所有的 POST ajax 调用都被发送到我的 NodeJS 服务器代码。 How I found this is by trying to POST using Postman.我是通过尝试使用 Postman POST 发现的。 All POSTMAN call requests were received by the server code but not the Express client requests.所有 POSTMAN 调用请求都由服务器代码接收,但不是 Express 客户端请求。 I checked by printing the console.log server debug statements to compare the findings between POSTMAN & Express Client.我通过打印 console.log 服务器调试语句进行检查,以比较 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.问题 #2 - 即使服务器响应状态为 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();在 ajax 调用中,我添加了 e.preventDefault(); where 'e' is the event which is triggered via #click/#onclick.其中“e”是通过#click/#onclick 触发的事件。
  2. Within ajax function call, I added the parameter dataType: 'json'.在 ajax 函数调用中,我添加了参数 dataType: 'json'。
  3. In the NodeJS server code, index.js, I replaced res.send(req.body.search_data);在 NodeJS 服务器代码 index.js 中,我替换了 res.send(req.body.search_data); with res.json(req.body.search_data);与 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. PS - e.preventDefault() 实际上是为不同的目的而构建的,但它有帮助,因为它可以防止链接跟随解决上述问题的 URL。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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