Uncaught SyntaxError: Unexpected token ILLEGAL on php json_encode - javascript

When I do this:
onClick = "return generateClient('<?php echo json_encode($_POST)?>');"
I am getting this error on the google chrome console.
Uncaught SyntaxError: Unexpected token ILLEGAL
But when I do this from php:
print_r(json_encode($_POST));
I get:
{"lang-select":"C++","question-id":"1","method-name":"Rishi","param-count":"1","lib-path":"c:\\h\\b.out","return-select":"unsigned int","sample-count":"1","class-name":"m"}
What can be the fault in this?

Your data contains " characters.
Your attribute values are delimited by " characters.
The first " in the data will end the attribute value.
A validator would have picked this up for you.
Run your data through htmlspecialchars to encode the quote marks.

Related

How to escape html tags in text

I have something like:
<td><script>document.write("${name}");</script></td>
when the name is equals to for example </script>test
then I got errors in console with: Uncaught SyntaxError: Invalid or unexpected token
if name is equls to <script>asda<script> then I got Uncaught SyntaxError: Unexpected token '<'
How can I escape those HTML tags and display text as it is ? I want this text to be interpreted as plain text, nothing more but weird situations happens when I put something special under the name.
If the input consists of special characters like for example '[]pol' then everything is fine.
< ("less than") is the HTML entity for "<", and > ("greater than") for ">"
<script> = "<script>"

Why it is showing error when parsing JSON having multiple properties?

Code:
JSON.parse('{"name":"dibya","company":"wipro}');
Error :
M208:1 Uncaught SyntaxError: Unexpected end of JSON input
at JSON.parse ()
at :1:6
It throws an error beacuse it is not a valid json. Should be:
JSON.parse('{"name":"dibya","company":"wipro"}');
You can check if it is valid on https://jsonlint.com/

Error parsing JSON data - "Uncaught SyntaxError: Unexpected token ."

I'm getting the error below in Chrome while parsing a JSON data. The data sample is at http://jsoneditoronline.org/?id=31ffc7c0e7e1a9a2adf641306497b57a This is a valid JSON and my server is sending the correct Content-Type value (application/json).
Uncaught SyntaxError: Unexpected token .
Firefox reports a slightly different message but it all points to the presence of the period (.) in the beginning of the content.
SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 6 of the JSON data
I've tried both $.parseJSON() and JSON.parse() methods.
What's the cause of this error? Please enlighten.
I've read the other similar posts here, but they refer to a different character like <, etc.,
EDIT: This is the piece of code I'm using to retrieve the server data.
$.ajax({
url : searchUrl
}).done(function(data) {
var json_array = JSON.parse(data); // Apparently data is already JSON parsed.
});
That data probably is already an object, try it without $.parseJSON() or JSON.parse() and it should work.

Uncaught SyntaxError: Unexpected token i

a = JSON.parse('["<input class="image" type="file" name="%simg_%d"/>"]');
gives the above error. Can someone explain why? Is %s the start of some kind of special character string in json/javascript?
You don't escape the quotes so you keep breaking in and out of the string which makes for invalid JSON
JSON.parse('["<input class=\\"image\\" type=\\"file\\" name=\\"%simg_%d\\"/>"]');

Parsing simple JSON using Ext gives SyntaxError: Unexpected token ILLEGAL

i'm doing a simple parse of some JSON and it's giving me an error in chrome, what am i missing here?
Ext.util.JSON.decode("{info: {synopsis: 'test'}}");
SyntaxError: Unexpected token ILLEGAL
Thanks a lot
http://www.json.org/
Think you should use double quotes instead of single quotes.
Ext.util.JSON.decode('{"info": {"synopsis": "test"}}');
Be careful if you are using ExtJs 4 onward, You have to use
Ext.JSON.decode('{"info": {"synopsis": "test"}}');

Categories

Resources