I understand this question has been asked many times but I am still not able to get the error. I am trying to parse text coming from server.
"MARUTI"
"MAHINDRA AND MAHINDRA"
"FIAT"
Every texts parsed except "MAHINDRA AND MAHINDRA". I do not understand where the problem is. here is my code.
var myObject = JSON.parse(httpxml.responseText);
Error in Console
Uncaught SyntaxError: Unexpected token D
Please help.
Thats not a JSON String. You should inform about JSON
http://en.wikipedia.org/wiki/JSON
Return a JSON string from your server. With PHP you can use json_encode to encode an array to a json string.
Related
I'm so frustrated with this.
I'm using Angular from CDN and I'm calling a $http.get request to a PHP file that prints all the result set (mysql) in JSON format, which is "well done".
But, I have stored some backslashes into the database as part of the value of a field. E.G:
{"Field 1":"user1"},{"Field 2":"some_data\2"}
That crashes the entire page and the data in the view is not being displayed. It's printed in the raw JSON file this way:
{"Field 2":"some_data�2"}
Currently I'm printing the resultant json "manually":
$outp = '{"Field 2":"'.$resultset['field_data'].'"}';
echo $outp;
I've tried everything I know: addslashes(), stripslashes(), htmlspecialchars(), json_encode()... Error is:
SyntaxError: Unexpected token in JSON at position 138742
Can anyone help me, please?
For testing purposes, I wish to use Ajax to request some JSON from the server. From the Ajax client's perspective, the JSON should look like:
json=[
{"source":"pa","jsonstring": '{"a":1,"b":2,"c":3}'},
{"source":"pa","jsonstring": '{"a":1,"b":2,"c":3}'},
{"source":"pa","jsonstring": '{"a":1,"b":2,"c":3}'}
];
Note that jsonstring is not JSON, but a string, and $.getJSON() should not parse it into an object.
My attempt is below, however, I get error Parse error: syntax error, unexpected ',' in /var/www/test/src/classes/Ajax.php on line 13.
How should this be performed?
$content=file_get_contents('../buffer.json',true); //Line 13
$buffer=$content?json_decode($content):[];
$json=json_encode(['a'=>1,'b'=>2,'c'=>3]);
$buffer[]=[
'source'=>'pa',
'jsonstring'=>'"'.$json.'"'
];
$buffer=json_encode($buffer);
file_put_contents('../buffer.json',$buffer);
header('Content-Type: application/json');
echo($buffer);
buffer.json output is shown below:
[{"source":"pa","jsonstring":"\"{\"a\":1,\"b\":2,\"c\":3}\""},{"source":"pa","jsonstring":"\"{\"a\":1,\"b\":2,\"c\":3}\""}]
Have you tried removing the extra quotes from 'jsonstring'=>'"'.$json.'"'? If you json_encode it (which it looks like you do), then it is already a string. I think it should be 'jsonstring' => $json.
I creating an app in which I'm using php in server side.When an Ajax call is made its calling an php to retrieve the data from the table but the issue is some table contains html & special characters tags so i get an error in the Ajax :
05-31 00:59:07.670: D/CordovaLog(872): Server is not responding... Please try again: SyntaxError: Unexpected token <
05-31 00:59:07.670: I/chromium(872): [INFO:CONSOLE(342)] "Server is not responding... Please try again: SyntaxError: Unexpected token <", source: file:///android_asset/www/home.html (342)
can any tell how to solve this issue.
Thanks in advance.
you may use escapeHtml which Returns an HTML escaped representation of the given plain text.
For more info see at escapeHtml
Or you may use TextUtils.htmlEncode("your json tag") to encode html.
Edit
As you are using phonegap so you may use htmlencoding is PHP also as
htmlentities($str, ENT_QUOTES);
For more information see at htmlentities in PHP
To encode html in php and decode back in your app,you may refer this link
Using PHP’s JSON encode and decode functions to handle data sent to and from your app
I have a PHP script that is building the header portion of an HTML(5) document. Part of what is being emitted is something like this
$rtn = <<<RTN
<script type='text/javascript'>
var _scrolls = {"alpha":{"cursorborder":"1px dashed rgba(200,13,57,1)"}};
var _floors = new Array(10000,{$mqt},{$mqf});
</script>
RTN;
The JSON is valid - at least, JSONlint.com seems to think so. However, in my browser (Chrome on Windows) this throws up an error Uncaught Syntax Error: Unexpected token illegal.
By dint of some experiment I have narrowed down the problem to the last attribute
"cursorborder":"1px dashed rgba(200,13,57,1)"
For some reason the browser is taking exception to the spaces in the attribute value. If I collapse that string so that it reads
"cursorborder":"1pxdashedrgba(200,13,57,1)"
the syntax error message disappears. I cannot think of any rational explanation for this. For completeness I guess I should mention that the JSON is being generated server side courtesy of json_encode.
Perhaps someone out here can tell me where I am going wrong?
it doesn't throw an error if I remove the first and the last ' , like this:
var _scrolls = {"alpha": {"bouncescroll":true,"boxzoom":false,"enabletranslate3d":true,"dblclickzoom":true,"gesturezoom":true,"hwacceleration":true,"horizrailenabled":true,"enablekeyboard":true,"railalign":"right","enablemousewheel":true,"nativeparentscrolling":true,"enablescrollonselection":true,"sensitiverail":true,"smoothscroll":true,"spacebarenabled":true,"railvalign":"bottom","touchbehavior":false,"autohidemode":false,"cursorcolor":"rgba(245,146,30,1)","background":"rgba(127,255,142,1)","cursorborder":"1px dashed rgba(200,13,57,1)"}};
You can't have literal line breaks in a JavaScript string literal.
Remove or escape them. Better: don't use JSON for this and just have a JavaScript object literal, that will save you from having to have a separate parse step.
As Jonas said, remove the ' from the beginning and the end of the string.
That way, the variable will become a valid Javascript Object and not just a long string containing JSON.
I'm moving php code from one linux server to another, and the new one is producing rubbish.
In Firebug, the first result looks great, like normal json encoded values, and works.
The second result (new server) is preceded by "html" and "body" tags, a "p" tag, and then "quot;" instead of actual quotes around the data (sorry, I couldn't get the form to display all that), and throws an error.
In both cases, the code is the same, the output an array passed through json_encode.
Could this be a configuration error?
Thanks in advance.
John
Well, ultimately I added header('Content-type: application/json'); which removed the encoded html. I'd sure like to know why I had to do that.