I am calling a YUI function to get status message from a php function that return the proper status in json format.
when i trigger the event in gives the error message :-
"Syntax error unexpected token u"
Here is the source :-
var ajax_url = 'initcalls.php?action=ping';
var pngbtn = Y.one('#id_solr_btn_ping');
pngbtn.on('click', function(e) {
Y.one('#solr_ping_status').setHTML(' <img src="pix/ajax-circle.gif">');
Y.io(ajax_url, {
on : {
success : function(data) {
try
{
var resp = Y.JSON.parse(data.responseText);
}
catch (e)
{
alert(e);
return;
}
if (resp.status == 'ok') {
Y.one('#solr_ping_status').setHTML(' <img src="pix/success.png">');
timeout("clearSaveStatus('#solr_ping_status')",2000);
}
else {
Y.one('#solr_ping_status').setHTML(' <img src="pix/warning.png">');
}}
}
});
return false;
});
My php file return the following result:-
{"status":"ok"}
Callbacks for Y.io
receive two parameters: the id of the transaction and the response object in the second parameter:
Y.io(url, {
on: {
success: function (id, response) {
// ...
}
}
});
The error you're getting is probably because you're accessing the responseText
property of id
which is undefined. The undefined value gets coerced to the "undefined"
string and JSON.parse
throws when encountering the u
in "undefined".
You just need to use the second parameter as your data
object.