Weird JSON JavaScript Problem in Rails

Weird JSON Javascript problem in Rails

This is Rails html-encoding your string as is default in Rails 3.

You need to mark your JSON as html_safe:

var stuff = <%= @json.to_s.html_safe %>

Note that .to_s is needed because as_json gives Hash instead of string. You could do this instead:

# in controller
@json = @nodes.to_json(:only => [:ID, :Lat, :Lon])

#and in view
var stuff = <%= @json.html_safe %>

Rails: Syntax error: JSON.parse: unexpected character at line 2 column 1 of the JSON data

You need to use JSON.stringify to first serialize your object to JSON, and then specify the content-type so your server understands it's JSON.

$.ajax({
url: "/posts/view",
type: "POST",
dataType: "json",
headers: {
'Content-Type': "application/json"
},
data: JSON.stringify({ post_id: id }),

The syntax error while parsing is raised because Rails can't recognize passed data (since it's not a valid JSON string).

  • contentType specifies the format of data being sent to the server as
    part of request
  • dataType specifies the expected format of data to be received by the client(browser).

Also change the method from GET to POST. GET requests (at least usually) do not have a message body. If you're set it to GET it will be converted to parameters of URL string.

Embedding Rails DB as_json.to_json into Javascript not working. (turns into " stuff)

Your OUR_SELLER_DB is an object. Objects do not indeed have method push. Did you mean it to be an array?

So why is it changing itself into some crazy form?

That "crazy form" is called "html escaping". And if you don't do that by default, bad things will happen. Try this to bypass escaping:

<%= raw p.to_json %>

Rails Ajax Unexpected token in JSON at position 0

I think you're missing the accept header in your Ajax request so you're actually getting the HTML page back again.

Try:

$.ajax({
dataType: 'json',
...
});

json, rails, parse error in javascript

You're treating a .js file like an .erb file - it's not. JavaScript files aren't parsed by Ruby - anything in public is sent as-is.

One way to get @poop into your JavaScript is to have a Rails action that renders json, then load that with XHR.

Alternatively, you could output @poop in the HTML template in a <script> tag, then load the contents of that script tag from your JavaScript.

Rails render JSON using %= not working as expected

Your code should look something like this,

respond_to do |format|
format.html index.html.erb
format.xml { render :xml => @organism.chromosomes) }
format.json { render :json => @organism.chromosomes) }
end

How to access JSON from a rails @variable?

I vaguely remember doing something like this before. The problem was that I was getting a json string from a web service and then calling .to_json on it.

Try changing this:

Hash.from_xml(test).to_json

to this:

Hash.from_xml(test)

If that doesn't work, I think that this other post should help you format your json correctly:
Weird JSON Javascript problem in Rails



Related Topics



Leave a reply



Submit