0

When a JSON is returned from an API call in my Rails app I get this response:

"{\"stdout\":\"2\\n\",\"stderr\":\"\",\"wallTime\":241,\"exitCode\":0}"

When it should look like this:

{"stdout": "2\n", "stderr": "", "wallTime": 241, "exitCode": 0}

How do I change this bearing in mind the response is being dealt with in JavaScript?

The JS handling the response:

$(document).ready ->
  $(".edit_code_lesson").on("ajax:success", (e, data, status, xhr) ->
    result = JSON.parse(data)
    alert(result);
  ).bind "ajax:error", (e, xhr, status, error) ->
    console.log(status + '\n ' + error);
    console.log(xhr);

It is after inspecting the xhr I noticed the HTML formatted JSON was being returned. A Syntax error is being thrown.

This is the controller fetching the API:

def evaluate
  @code_lesson = CodeLesson.find(params[:code_lesson][:id])
  @language = Language.find(@code_lesson.language_id).slug
  @sandie = Sandie.new(language: @language)
  @code = @sandie.evaluate(code: params[:code_lesson][:user_code]).to_json
end
2
  • are you using the JSON module or hand-building? Commented Feb 13, 2014 at 18:43
  • @dandavis don't quite understand the question? I'm using the API wrapper Sandie Commented Feb 13, 2014 at 18:45

2 Answers 2

1

In your controller, do this:

render :json => your_params, :status => 200
Sign up to request clarification or add additional context in comments.

Comments

0

Where exactly does it appear like that? Already in the controller? Or somewhere on your site after you render it? You can render with

<% raw @some_stuff %>

or

<%== @some_stuff %>

to avoid such conversions. (If @some_stuff is already a string). What do you want to do there? just render it as HTML or generate a JSON object?

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.