Question

given the following: where data is a string received as an AJAX value and data has a value of 'good':

console.log data          # good
console.log typeof data   # string
console.log typeof 'data' # string

if data is 'good'
  console.log 'test' # NOTHING!!!

I don't understand...

makes me want the good ol' JavaScript back...

A more complete example after a suggestion:

The following does not work either...

  $('#profile_slug').keyup(()->
    if $(this).val() != original_slug
      value = encodeURIComponent $(this).val()
      console.log value
      $.get('/live_validate/slug?slug='+value, (data)->

        console.log data
        console.log typeof data
        console.log typeof 'data'

        `
        if (data == 'good') {
          console.log('test');
        }

       `

update 2

oddly enough:

console.dir data
console.dir 'good'

good
No Properties

good
No Properties

update 3

controller

  if @slug.nil?
    @message = "good"
  else
    @message = "bad"
  end

view

!= @message

coffeescript

new_data = data.replace /^\s+|\s+$/g, ""

result

same sh*t.

update 4 + answer

This code ended up being what I needed, at first it didn't work quite well with everything else I had going on, but it is the final piece in the puzzle, allowing me to make sure there are no invisible spaces before or after the received string.

    $.get('/live_validate/slug?slug='+value, (data)->
        # console.log data

        stripped_data = data.replace /^\s+|\s+$/g, ""

        # console.log encodeURIComponent data
        # console.log (data.charCodeAt(i) for i in [0...data.length])       


        if stripped_data is 'good'
          $('#profile_slug').addClass('valid-field')
          $('#profile_slug').removeClass('invalid-field')
        else if stripped_data is 'bad'
          $('#profile_slug').addClass('invalid-field')
          $('#profile_slug').removeClass('valid-field')
Was it helpful?

Solution 2

It ended up being an invisible character at the end of the ajax response string... thanks all!

OTHER TIPS

This is weird, but my guess is that one of the two 'good' strings in the comparison is using Unicode characters that resemble, but are not equal to, the ASCII characters you're expecting.

Try this:

console.log (data.charCodeAt(i) for i in [0...data.length])

When data = 'good', I get

[ 103, 111, 111, 100 ]
Licensed under: CC-BY-SA with attribution
Not affiliated with StackOverflow
scroll top