i have the following button :
<button onclick='enroll(<?php echo $course['cs_id']; ?>)'>enroll</button>
when the user click on it the following javascript function will start :
function enroll(id){
$.post("<?php echo base_url().'courses/enroll/'; ?>"+id , function(data){
obj = JSON.parse(data);
alert(obj.state);
});
}
which perform a post request to php page which echo json array like this :
$data = array("state"=>"done");
die(json_encode($data));
but unfortunately the following error appears in the console :
JSON.parse: unexpected character
i checked the page response and its look like this (which seems fine ) :
{"state":"done"}
also when i change the enroll() function to be like this :
function enroll(id){
$.post("<?php echo base_url().'courses/enroll/'; ?>"+id , function(data){
alert(data);
});
}
its show the alert message correctly like this :
{"state":"done"}
so why it refuse do parse the response where everything seems okay ? any idea ?
My guess is that your code is actually working better than you think! I think that jQuery receives your JSON from the server and realises that it is JSON, even though you haven't specified it as such. (Perhaps your PHP is sending a Content-Type header?)
So data is actually already a Javascript object, which jQuery has decoded from your JSON. When you call JSON.parse on an object, it is converted to a string [object Object], and then parsed. [object Object] is evidently invalid JSON, hence the unexpected character error.
Since the string was obviously valid JSON, my next guess was that there was some data before the string that was causing the parser to be unhappy. The best way to test this is with String#charCodeAt, which reveals the precise character at a given point in the string.
So, with the following code, we can test to see what the first character in the string is:
console.log(data.charCodeAt(0));
You say that this returns 65279, which, at the beginning of a Unicode document, is a byte order mark. You are apparently outputting this somewhere in your server-side code. You need to find out where that is and remove it.
Do not parse the data again:
function enroll(id){
$.post("<?php echo base_url().'courses/enroll/'; ?>"+id , function(data){
alert(data.state);
});
}
Related
Good morning everyone and thank you in advance for any suggestions. I have written a small web application to perform simple searches in a stamps database using php and javascript.
The server sends to the browser the whole database as a JSON and the queries are done client-side with a javascript code.
The JSON has this structure:
{"ck":0,"db":[["string11","string12","string13"],["string21","string22","string23"], etc... } .
Until now the system has worked perfectly and over 1500 stamps could be shown.
Suddenly it stopped working and, in the browser's Javascript console, this error message appeared:
VM672:1 Uncaught SyntaxError: Expected ',' or ']' after array element in JSON at position 97506 at JSON.parse (<anonymous>) ...etc...
After a series of tests, by exclusion I came to discover that it was the word "annullo" in the last added record to generate the error.
I guess it could be the substring "null" to give problems, but I have no idea how to escape it.
A really strange thing is that, whilst failing with the JSON.parse() function, browser's javascript console, as well as other json validation tools, recognise the server's response as a valid JSON.
Thanks for any help!
As #Andrea Soffiantini pointed in a comment, this was the actual problem:
Client side I had this command a few lines before parsing: data = data.replaceAll("null","\"\""); , where data is the json as a string, as received from server.
This replaces the string "anullo" with "a""o", which is invalid in JSON syntax.
I think I understand the problem. All strings are enclosed in quotes.
{example: "example"}
In the development of this case they must have put "annullo" but they forgot to insert the quotes. If it was null, not a string, it could look like this, without quotes.
json = {example: null}
However, as annullo is an invalid word in javascript and is not as a string, it gives an error.
The correct thing to do is to correct the error in the source, transforming it to null, however, I believe that you can also use this regular expression. But do tests to ensure that there is no undue substitution elsewhere.
json.replace(/.+["']: ?annull(o|are)[\n,}]$/g, matched => {
return matched.replace(/annull(o|are)/, 'null')
})
I am getting a "Uncaught SyntaxError: missing ) after argument list" error when running the line below (which returns a json formatted file from my PHP to my javascript).
var moredata = JSON.parse("<?php echo json_encode(find_detailed_forecast()); ?>");
I'm not sure how to fix this. If I edit the line to remove the parentheses or add a '' pair, then I get a "Not Found The requested URL /[object Object] was not found on this server." When I look at the console, the php echo code is running correctly, so I get a JSON.parse(""long string here""). Not sure what is exactly wrong with my javascript and any help would be much appreciated. If it matters, my php, javascript, and html code are all in the same file.
Edit: The code looks like below on execution (its a really long file so I wasn't sure if I should copy paste the entire thing):
var moredata = JSON.parse(""{\"latitude\":999.123456,\"longitude\":-999.123456,\"timezone\":\"America\/New_York\",\"currently\":{ .... }"\n"");
JSON.parse outputs data with double quotes, so you have to use single quotes.
var moredata = JSON.parse('<?php echo json_encode(find_detailed_forecast()); ?>');
Here's a simplified PHP side of my ajax context:
// in a for loop
$text = "/home/ubuntu/CANE-HDD-100/y.txt"
// $text_encoded = urlencode($text);
// $text_encoded = preg_replace("/(\-)/", "%2D", $text_encoded);
$text_encoded = "%2Fhome%2Fubuntu%2FCANE%2DHDD%2D100%2Fy.txt"
$structure[$i] = $text_encoded;
echo json_encode($structure);
And here is the JS side:
request.onload = function()
{
var cleanedText = JSON.parse(this.responseText);
alert(cleanedText);
};
That throws the following error:
SyntaxError: JSON.parse: unexpected character at line 1 column 1 of the JSON data
If I substitute JSON.parse(this.responseText) with decodeURI(this.responseText), I get cleanedText equal to
/home/ubuntu/CANE-HDD-100/y.txt
["%2Fhome%2Fubuntu%2FCANE-HDD-100%2Fy.txt"]
which I dislike, since an hypotetic for loop on cleanedText would treat every element of that variable (correctly) as a character, while I'd obviously like to get elements as whole URLs.
A possible, dirty workaround is to set up some regex on cleanedText to recover every URL, but I'm wondering if there would be a much cleaner way.
w3re's comment has to be correct. You must have malformed JSON. We can show this by taking the output you do get from decodeURI, running it back through encodeURI to get what must therefore be your original string from PHP, and then trying JSON.parse on it. We get an error:
JSON.parse(encodeURI('/home/ubuntu/CANE-HDD-100/y.txt ["%2Fhome%2Fubuntu%2FCANE-HDD-100%2Fy.txt"]'))
(program):1 Uncaught SyntaxError: Unexpected token /
That error is from Chrome, but the :1 is saying 'line 1' and the / is at column 1, so I think that's the error you're seeing.
Basically, this string is not valid JSON, because of the all the stuff at the start which is not wrapped in quotes.
However, for example, this will work, if you put it all in quotes and escape the existing ones:
JSON.parse('"/home/ubuntu/CANE-HDD-100/y.txt \\"%2Fhome%2Fubuntu%2FCANE-HDD-100%2Fy.txt\\""')
or using an array:
JSON.parse('["/home/ubuntu/CANE-HDD-100/y.txt", "%2Fhome%2Fubuntu%2FCANE-HDD-100%2Fy.txt"]')
or an object:
JSON.parse('{"/home/ubuntu/CANE-HDD-100/y.txt": "%2Fhome%2Fubuntu%2FCANE-HDD-100%2Fy.txt"}')
so something must be going awry in your PHP or JavaScript in and amongst your JSON encoding and decoding. To help further, I'd need to see more of your code.
I've spent the last few days trying to get around this problem. Every time I try to get a JSON object using Angular's $http.get, I get "Error: JSON.parse: unexpected character".
My JSON is created using the PHP's json_encode. The PHP code is the following:
header('Content-Type:application/json');
echo json_encode($to_encode);
The JSON it creates looks like this:
{"id":"1","para":{"para_id":"1","story_id":"1","para_content":""\u017bagiel na horyzoncie!" - s\u0142ycha\u0107 krzyk z bocianiego gniazda. Fregata Jego Kr\u00f3lewskiej Mo\u015bci "Acanta" \u017cwawo sunie przez b\u0142\u0119kit oceanu. Ty, jej dow\u00f3dca, stoisz na rufie. Bosman i sternik patrz\u0105 si\u0119 na ciebie, oczekuj\u0105c rozkaz\u00f3w."},"tunnels":[{"tunnel_id":"1","start_para_id":"1","end_para_id":"2","tunnel_content":"Spokojnie, zbli\u017cmy si\u0119 i sprawd\u017amy, kto to."},{"tunnel_id":"2","start_para_id":"1","end_para_id":"3","tunnel_content":"Bi\u0107 na alarm! Za\u0142oga do dzia\u0142!"}]}
(It's UTF8-encoded Polish, if it's relevant; the text is just a placeholder)
The JS that gets it look like this:
$scope.current_para = $http.get("/gamebook/run");
$scope.current_para.then(function(os){result = os.data; console.log(result);}
Instead of a neat JSON in my console.log, I get the error, even though my Firebug tells me that the GET method was successful and even shows me the JSON it received.
I've finally found a workaround to this problems - I still don't know what or why happened, but here's what could be done.
On the server side you have to wrap your JSON data in single quotes (making it a string) then use eval() in JS to turn it back into JSON.
I know it's not elegant, but that's the only solution I've found. Hope it'll help someone.
Thanks everyone for the input!
I have an ajax function that responds with contents of an html tag, as a json string that I will eval to make an object out of it.
Now as long as the json string has no javascript inside it, it works fine. But when I include some simple javascript inside the string, I get Uncaught SyntaxError: Unexpected identifier when I try to eval the string, although I escape every ' and ".
Here is the sample: http://jsfiddle.net/dCtA3/2/
When I remove the javascript: http://jsfiddle.net/dCtA3/3/ it works.
Try it using proper JSON (if your XHR returns JSON, use that, parse it, or check if the browser didn't parse it already for you (should be [response].innerhtml)). Change your jsfiddle code to:
var contents = JSON.stringify(
{innerhtml : "<p onclick=\"javascript:alert('hello world');\"> click me! </p>"}
);
function insertContent() {
document.getElementById("container")
.innerHTML = JSON.parse(contents).innerhtml;
}
[Edit based on comment]
see this jsfiddle where a json request is simulated on button click. The stringified response object is shown after clicking the button.
Do you need your javascript code to be generated and does it HAVE TO be in that string? If the javascript can be in your js file you can simply do this:
make a function helloWorld that does the alert, and in your json string with html hance onclick="helloworld()".