我收到此错误:
错误:错误:SyntaxError:JSON.parse:意外字符
_serviceURl = sharepoint02 / LMSRest.svc / rest / GetDemoOAuthPlan?LMSUsername = bfolmer& oauth_consumer_key = Sec& oauth_nonce = 8765121& oauth_signature_method = HMAC-SHA1& oauth_timestamp = 1363238520& oauth_version = 1.0& PageNumber = 1& PageSize = 5& SortColumn =课程与安培; SortDirection =真&安培; oauth_signature = V35lYfuMNUxrzVWfUKdOchcbTEQ =“
如果从浏览器收到URL结果:
{
"MyLearningList": [
{
"LearningId": 26,
"Curriculum": "Administrative Law",
"CurriculumURL": null,
"TrainingStatus": "InProgress",
"StartDate": "10/15/2012",
"EndDate": "10/15/2013"
},
{
"LearningId": 2,
"Curriculum": "Architecture",
"CurriculumURL": null,
"TrainingStatus": "InProgress",
"StartDate": "10/15/2012",
"EndDate": "11/15/2013"
},
{
"LearningId": 11,
"Curriculum": "Audit",
"CurriculumURL": null,
"TrainingStatus": "InProgress",
"StartDate": "10/15/2012",
"EndDate": "10/15/2013"
},
{
"LearningId": 21,
"Curriculum": "Automobile Engg.",
"CurriculumURL": null,
"TrainingStatus": "InProgress",
"StartDate": "10/15/2012",
"EndDate": "10/15/2013"
},
{
"LearningId": 32,
"Curriculum": "B. A. M. S. (Bachelor of Ayurvedic Medicine and Surgery)",
"CurriculumURL": null,
"TrainingStatus": "InProgress",
"StartDate": "10/15/2012",
"EndDate": "10/5/2013"
}
],
"PageSize": 5,
"CurrentPageNumber": 1,
"TotalRecordCount": 39,
"TotalPageCount": 8,
"NextPageUrl": "sharepoint02:50000/LMSRest.svc/rest/GetDemoOAuthPlan?LMSUsername=bfolmer&PageNumber=2&PageSize=5&SortColumn=Curriculum&SortDirection=True",
"PreviousPageUrl": ""
}
以下是我的JavaScript代码:
function BindGrid<%=Tag %>(_serviceURL) {
//Append the Loading
$('#tbl<%=Tag %>').append('<tr class="loading"><td colspan="5"><img src="/Style Library/LMS/images/loading.gif" align="top" /> Loading...</td></tr>');
$.support.cors = true;
$.ajax({
type: "GET",
url: _serviceURL,
contentType: "application/json",
timeout: 17000,
dataType: "json",
success: function (_result) {
//Bind the Header
BindHeader<%=Tag %>();
if( _result == null || _result == "")
{
$('#tbl<%=Tag %>').append('<tr class="bottom"><td colspan="5" class="table-data-error">Error : Some problem fetching the data. Please contact IT team.</td></tr>');
return ;
}
///Catalog
var _myObject = (typeof _result) == 'string' ? eval('(' + _result + ')') : _result;
//Bind the values
$("input[id*='txtPageSize'][Tag='<%=Tag %>']").val(_myObject.PageSize);
$("input[id*='txtCurrentPageNumber'][Tag='<%=Tag %>']").val(_myObject.CurrentPageNumber);
$("input[id*='txtTotalRecordCount'][Tag='<%=Tag %>']").val(_myObject.TotalRecordCount);
$("input[id*='txtTotalPageCount'][Tag='<%=Tag %>']").val(_myObject.TotalPageCount);
$("input[id*='txtNextPageUrl'][Tag='<%=Tag %>']").val(_myObject.NextPageUrl);
$("input[id*='txtPreviousPageUrl'][Tag='<%=Tag %>']").val(_myObject.PreviousPageUrl);
//Bind the grid
if (_myObject.MyLearningList == null || _myObject.MyLearningList.length == 0) {
$('#tbl<%=Tag %>').append('<tr class="bottom"><td colspan="5" class="table-data-message">No course found.</td></tr>');
return;
}
//ITEM START
for (i = 0; i < _myObject.MyLearningList.length; i = i + 1) {
//Get the values
var Curriculum = _myObject.MyLearningList[i].Curriculum;
var _Status = _myObject.MyLearningList[i].TrainingStatus;
var _startDate = _myObject.MyLearningList[i].StartDate;
var _endDate = _myObject.MyLearningList[i].EndDate;
var _css = "odd";
if ((i + 1) % 2 == 0)
{ _css = "even"; }
//Append
$('#tbl<%=Tag %>').append("<tr class='" + _css + "'><td>" + Curriculum + "</td><td>" + _Status + "</td><td>" + _startDate + "</td><td>" + _endDate + "</td></tr>");
}
//ITEM END
//PAGING START
var _trFooter = "";
if( _myObject.TotalRecordCount > 0 )
{
if( _myObject.PreviousPageUrl != "" )
{
_trFooter = _trFooter + '<td style="width:25%;"><a href="javascript:Previous<%=Tag %>();"><< Previous</a><td>';
}
else{
_trFooter = _trFooter + '<td style="width:25%;"> <td>';
}
_trFooter = _trFooter + "<td style='width:50%;font-weight:bold;text-align:center;' nowrap='nowrap'>Page No : "+ _myObject.CurrentPageNumber +" of "+ _myObject.TotalPageCount +"</td>";
if( _myObject.NextPageUrl != "" )
{
_trFooter = _trFooter + '<td style="width:25%;text-align:right;"><a style="float:right;width:50px;" href="javascript:Next<%=Tag %>();">Next >></a></td>';
}
else{
_trFooter = _trFooter + '<td style="width:25%;"> <td>';
}
$('#tbl<%=Tag %>').append('<tr class="bottom"><td colspan="4" class="table-data-paging"><table cellspacing="0" border="0" cellpadding="0" style="width:100%;"><tr>'+ _trFooter +'</tr></table></td></tr>');
}
//PAGING END
},
error: function (XMLHttpRequest, textStatus, errorThrown) {
//Binds the header
BindHeader<%=Tag %>();
//Show the error
$('#tbl<%=Tag %>').append('<tr class="bottom"><td colspan="5" class="table-data-error">Error : Some error occoured fetching the data. Please contact IT team. Error :'+errorThrown+'</td></tr>');
}
});
}
答案 0 :(得分:0)
您的Json是正确的,您可以使用JSONLint进行验证。我还尝试使用JQuery parseJSON 方法解析它并且它有效。
代码示例
var _result =
'{"MyLearningList": [{"LearningId": 26,"Curriculum": "Administrative Law","CurriculumURL": null, "TrainingStatus": "InProgress","StartDate": "10/15/2012","EndDate": "10/15/2013"},{"LearningId": 2,"Curriculum": "Architecture","CurriculumURL": null,"TrainingStatus": "InProgress","StartDate": "10/15/2012","EndDate": "11/15/2013"},{"LearningId": 11,"Curriculum": "Audit","CurriculumURL": null,"TrainingStatus": "InProgress","StartDate": "10/15/2012","EndDate": "10/15/2013"},{"LearningId": 21,"Curriculum": "Automobile Engg.","CurriculumURL": null,"TrainingStatus": "InProgress","StartDate": "10/15/2012","EndDate": "10/15/2013"},{"LearningId": 32,"Curriculum": "B. A. M. S. (Bachelor of Ayurvedic Medicine and Surgery)","CurriculumURL": null,"TrainingStatus": "InProgress","StartDate": "10/15/2012","EndDate": "10/5/2013"}],"PageSize": 5,"CurrentPageNumber": 1,"TotalRecordCount": 39,"TotalPageCount": 8,"NextPageUrl": "sharepoint02:50000/LMSRest.svc/rest/GetDemoOAuthPlan?LMSUsername=bfolmer&PageNumber=2&PageSize=5&SortColumn=Curriculum&SortDirection=True","PreviousPageUrl": ""}';
var obj = $.parseJSON(_result);
alert(obj.PageSize);
alert(obj.NextPageUrl);
使用您的JSON here
的工作示例编辑1: 即使eval()也能正常工作
请参阅here