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.

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

报告相同问题?

悬赏问题

  • ¥15 VS2022创建MVC framework提示:预安装的程序包具有对缺少的注册表值的引用
  • ¥15 weditor无法连接模拟器Local server not started, start with?
  • ¥20 6-3 String类定义
  • ¥15 嵌入式--定时器使用
  • ¥20 51单片机学习中的问题
  • ¥30 Windows Server 2016利用兩張網卡處理兩個不同網絡
  • ¥15 Python中knn问题
  • ¥15 使用C#,asp.net读取Excel文件并保存到Oracle数据库
  • ¥15 C# datagridview 单元格显示进度及值
  • ¥15 thinkphp6配合social login单点登录问题