【问题标题】:parse error from ajax callajax调用解析错误
【发布时间】:2013-10-11 02:41:09
【问题描述】:

我已经使用开源库编写了一些 ajax 代码来进行 jquery 分页。 当页面首次加载时,它会正确查询并显示我数据库中的前 25 条记录。但是所有后续请求都失败并出现解析错误。 我看不出第一页中的数据格式与其他页面有什么不同。

我尝试使用 JSON Lint,但我的 json 都没有通过,即使是第 1 页的查询。 我的 json 数据如下所示:

"[{\"createddatetime\":\"2013-09-10 17:56:54\",\"description\":\"and the final update\",\"number\":\"72212\",\"updatedname\":\"28112\",\"createdname\":\"conversion script\",\"user\":\"28507\",\"position\":\"1\",\"device_id\":\"2\",\"user_id\":\"2\",\"password\":\"Wh16dteaR\",\"updateddatetime\":\"2013-10-07 15:14:28\"},{\"createddatetime\":\"2013-09-10 17:56:54\",\"description\":\"Bauer\",\"number\":\"72787\",\"createdname\":\"conversion script\",\"user\":\"28509\",\"position\":\"2\",\"device_id\":\"4\",\"user_id\":\"4\",\"password\":\"EHVOzIx1\"},{\"createddatetime\":\"2013-09-10 17:56:54\",\"description\":\" Woosly\",\"number\":\"72822\",\"createdname\":\"conversion script\",\"user\":\"28510\",\"position\":\"3\",\"device_id\":\"5\",\"user_id\":\"5\",\"password\":\"IP8rsdOE\"}]"

然后我使用 parseJSON 方法将上面的字符串转换成一个对象。 这是进行 ajax 调用和解析的主要例程:

   $.ajax({
      url: mypath + '?startpos=' + page_index * items_per_page + '&numberofrecordstograb=' + items_per_page + '&viewtype=json',
      dataType: 'json',
      success: function(data){                           
             data = $.parseJSON(data); //converting to a javascript object vs. just string...       
             if (data !=null) {   

                for(var i=0;i<data.length;i++){
                        var deviceobj = data[i];                        
                        newcontent = newcontent + "<TR>";
                        newcontent=newcontent + '<TD>';    

                        //add EDIT hyperlink
                        if ($("#editdevicesettings").val() == "true") {              
                            var temp  = $("#editlinkpath").val();
                            newcontent=newcontent +  temp.replace("xxx",deviceobj["device_id"]) + '&nbsp;&nbsp;';
                        } 

                        //add DELETE hyperlink
                        if ($("#deletedevice").val() == "true") {              
                            var temp  = $("#deletelinkpath").val();
                            newcontent=newcontent +  temp.replace("xxx",deviceobj["device_id"]);
                        }                                 
                        newcontent=newcontent + '</TD>';

                        newcontent=newcontent + '<TD>' + deviceobj["number"] +'</TD>';
                        newcontent=newcontent + '<<TD>' + deviceobj["user"] + '</TD>';
                        newcontent=newcontent + '<<TD>' + deviceobj["password"] + '</TD>';
                        if (deviceobj["name"]) {
                              newcontent=newcontent + '<TD>' + deviceobj["name"] + '</TD>';
                        } 
                        else  {
                             newcontent=newcontent + '<TD>&nbsp;</TD>';
                        }
                        newcontent=newcontent + '<TD>' + deviceobj["description"]  + '</TD>';
                        newcontent = newcontent + "</TR>";         
                }// end for 
                // Replace old content with new content
                $('#Searchresult').html(newcontent);                    
            }//end if

      },
      error: function(request, textStatus, errorThrown) {
        console.log(textStatus);

      },
      complete: function(request, textStatus) { //for additional info
        //alert(request.responseText);
        console.log(textStatus);
      }
    });

    // Prevent click eventpropagation
    return false;
}//end pageselectCallback()

我不确定如何解决此问题。 任何建议将不胜感激

【问题讨论】:

    标签: javascript jquery ajax jquery-pagination


    【解决方案1】:

    我决定将返回的记录数减少到每页一个...以缩小违规记录的范围。 现在我知道它是哪条记录了,我应该能够解决问题。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2017-07-31
      • 2012-05-14
      • 1970-01-01
      • 2016-07-01
      相关资源
      最近更新 更多