Home JSON.parse: unexpected character at line 1 column 1 of the JSON data on sending Javascript
Reply: 0

JSON.parse: unexpected character at line 1 column 1 of the JSON data on sending Javascript

user7770
1#
user7770 Published in September 20, 2018, 10:22 pm

I am working on a simple message box, using "remote: true", and then replacing a flash_message div with a success or failure message. For some reason this keeps on giving the error "JSON.parse: unexpected character at line 1 column 1 of the JSON data".

My view:

= form_for Message.new, remote: true, format: :js do |f|
  = hidden_field_tag :authenticity_token, form_authenticity_token
  = f.text_field :name, placeholder: 'Naam'
  = f.text_field :email, placeholder: 'E-mail'
  = f.text_area :message, placeholder: 'Jouw bericht'
  = f.submit

#flash-message

My controller:

def create
  @message = Message.new(params[:message])

  respond_to do |format|
    if @message.valid?
      # MessageMailer.contact(@message).deliver

      format.html
      format.js { flash.now[:success] = "Thank you for your message. I'll get back to you soon!" }
    else
      format.html
      format.js { flash.now[:failure] = "Something went wrong!" }
    end
  end
end

My create.js.erb:

$('#flash-message').html("<%= j render 'flash' %>").delay(3000).fadeOut(4000);

On the terminal I see:

Started POST "/messages.js" for 127.0.0.1 at 2018-05-10 16:14:33 +0200
Processing by MessagesController#create as JS
  Parameters: {"utf8"=>"✓", "authenticity_token"=>"/3VXW31YVYF/mfle+JiHHx1jRguLF4TkepPWGqXYhP4tc/qS2IfGWlWpvcW4A4pwwcTGmHZJHnpzajqT+ETLiw==", "message"=>{"name"=>"", "email"=>"", "message"=>""}, "commit"=>"Create Message"}
  Rendering messages/create.js.erb
  Rendered messages/_flash.html.slim (2.3ms)
  Rendered messages/create.js.erb (3.8ms)
Completed 200 OK in 19ms (Views: 11.8ms | ActiveRecord: 0.0ms)

And in the browser I get

SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data

$('#flash-message').html("<div class=\"alert alert-failure\">Something went wrong!<\/div>").delay(3000).fadeOut(4000);

I'm apparently doing something very wrong, but don't understand why my javascript code suddenly gets parsed as json.

I've been googling the error, but to no avail.

Any suggestions on what I might be doing wrong?

share|improve this question
  • Did you think of including the problematic JSON string in your question? – marekful May 10 at 14:24
  • @marekful - as far as I know I'm not using any JSON at all. Purpose is to just deliver some Javascript (see the second line in the browser feedback above), but that code is somehow interpreted as JSON...? And even if I replace that code by a simple javascript popup, I still get the same error... – Danny May 10 at 14:33

1 Answer 1

active oldest votes
up vote 0 down vote
You need to login account before you can post.

About| Privacy statement| Terms of Service| Advertising| Contact us| Help| Sitemap|
Processed in 0.287918 second(s) , Gzip On .

© 2016 Powered by mzan.com design MATCHINFO