FridmanChan
FridmanChan

Reputation: 130

Empty values on Ajax response using coffeescript

I've got ajax in the coffee script - which send values to back-end. In browser-panel I could see response with json inside, but on success - all variables is undifined. Could somebody help with it?

here is code of the ajax.

    $.post '/articles/' + id + '/comments',
  contentType: 'application/json'
  data: comment_params:
    commenter: commenter
    body: body
  success: (data, textStatus, jQxhr) ->
    console.log(textStatus)
    $('#comments').append JSON.stringify(data)
  dataType: 'json'

All variables data, textStatus, jxQhr are undifined. How could I get this values from those variables?

Upvotes: 1

Views: 144

Answers (1)

max pleaner
max pleaner

Reputation: 26768

You should look at the docs for what you're trying to do:

$.post

$.ajax

This is two ways to perform a post, but they have different method signatures.

Like is said in a comment, what you have here is a mix of the different syntaxes, which isn't valid for either.

For the most part, it looks like the $.ajax signature, so you can change it slightly like so (note I also fixed the indentation - try and get this right when you post code in a question, since it's significant for languages like coffeescript):

# Note i'm using string interpolation, not concatenation
$.ajax "/articles/#{id}/comments",
  # add this key-val to determine the request type
  method: "POST"
  contentType: 'application/json'
  data: comment_params:
    commenter: commenter
    body: body
  success: (data, textStatus, jQxhr) ->
    console.log(textStatus)
    $('#comments').append JSON.stringify(data)
  dataType: 'json'

Upvotes: 1

Related Questions