dongxie3681 2013-06-23 18:57
浏览 72
已采纳

语法错误意外令牌u Json错误

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('&nbsp;<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('&nbsp;<img src="pix/success.png">');
           timeout("clearSaveStatus('#solr_ping_status')",2000);
        }
        else {
        Y.one('#solr_ping_status').setHTML('&nbsp;<img src="pix/warning.png">');
        }}
        }
              });

            return false; 

           });

My php file return the following result:-

     {"status":"ok"}
  • 写回答

1条回答 默认 最新

  • duanliaouu965826 2013-06-23 19:45
    关注

    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.

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?