1

When I return the value of a AJAX form that submits an API I get this:

SyntaxError: Unexpected token &

The AJAX response handling looks like this:

$(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);

After looking at the responseText from the xhr I can see the response is:

"{"stdout"=>"2\n", "stderr"=>"", "wallTime"=>237, "exitCode"=>0}"

I am new to HTML so I am unsure if the response should be in HTML. How can I fix this?

NOTE: This is a Rails App using an API wrapper.

2
  • 2
    That isn't valid JSON. You need to make your server not HTML-encode text that isn't being inserted into HTML. Commented Feb 13, 2014 at 17:36
  • the response looks like json that has been passed through a funciton that HTML-escapes special characters... Commented Feb 13, 2014 at 17:37

1 Answer 1

3

Even if the HTML entities were corrected*, that still wouldn't be JSON. It would look like this:

{"stdout"=>"2\n", "stderr"=>"", "wallTime"=>237, "exitCode"=>0}

whereas JSON would look like this:

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

You'll need to fix the server side of this so that: 1. It doesn't do HTML-encoding, and 2. It does output JSON, rather than that PHP-like syntax. (If you're using PHP, you can output JSON using json_encode.)


(* You don't HTML-encode JSON, normally. The only time you might do that would be if you were giving an example of JSON in an HTML page, and wanted to make sure it came out looking right [just like any other text].)

Sign up to request clarification or add additional context in comments.

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.