简体   繁体   English

jQuery的.done()和.always()永远不会被调用

[英]jQuery's .done() and .always() never getting called

I'm trying to do a simple ajax api call, but my jQuery .done() and .always() callbacks are never getting run, even after a successful call. 我正在尝试做一个简单的ajax api调用,但即使在成功调用之后,我的jQuery .done().always()回调也永远不会运行。

Here's where I do the api call: 这是我进行api调用的地方:

join: (e) ->

    e.preventDefault()
    email = $('.email').val()

    $.ajax(
      type: 'POST'
      url: '/api/email'
      data: { email: email }
    ).done((msg) ->
      console.log 'POST success'
      console.log msg
    ).always((msg) ->
      console.log 'Always'
    )

Here's the post from my controller: 这是我的控制器的post

app.post '/api/email', (req, res) ->

    console.log req.body

The problems that my server is correctly receiving the data from the api call, but my jQuery callbacks never get fired. 我的服务器正在从api调用中正确接收数据的问题,但我的jQuery回调永远不会被触发。

{ email: 'test@email.com' } // What gets logged server-side

EDIT 编辑

So I tried adding a .fail() method and switched up the code a bit to see if my syntax was wrong, but this new code is still functioning like the old code. 所以我尝试添加.fail()方法并稍微调整代码以查看我的语法是否错误,但这个新代码仍然像旧代码一样运行。

request = $.ajax(
  type: 'POST'
  url: '/api/email/beta'
  data: { email: email }
)

request.done( (msg) ->
  console.log 'POST success'
  console.log msg
)

request.fail( (msg) ->
  console.log 'POST Fail'
  console.log msg
)

request.always( () ->
  console.log 'Always'
)

EDIT 2 编辑2

I just realized that the POST is actually "failing", but after 4.1 mins... But the weird thing is that I'm still getting the data in the req.body object...? 我刚刚意识到POST实际上是“失败”,但是在4.1分钟之后...但奇怪的是我仍然在req.body对象中获取数据......?

Here's a screenshot of network from chrome tools: 这是Chrome工具的网络截图:

在此输入图像描述

Looks like you receive the request at the server (and log it), but then don't do anything about it. 看起来你在服务器上收到了请求(并记录下来),但是后来却没有做任何事情。 This eventually leads to a timeout. 这最终导致超时。 Change it to 将其更改为

app.post '/api/email', (req, res) ->
    console.log req.body
    res.write(JSON.stringify({success:true})/* or whatever*/);
    res.end();

Maybe you're using some framework with special methods on the res object; 也许你在res对象上使用了一些带有特殊方法的框架; if so then look up its documentation about how to answer a request. 如果是这样,那么查看有关如何回答请求的文档。

Try like this: 试试这样:

$.ajax({
      type: 'POST'
      url: '/api/email'
      data: { email: email}
    }).done((msg) ->
      console.log 'POST success'
      console.log msg
    ).always((msg) ->
      console.log 'Always'
    )

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

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