parseError,语法错误:对Sharepoint的jquery Json查询的无效字符

时间:2015-10-28 14:01:28

标签: jquery json sharepoint sharepoint-2013

我正在尝试使用项目found here。我的列表配置类似于教程中的列表。它不适合我。我将问题跟踪到我的JSON查询。

我重写了JSON查询以符合jQuery网站上的示例。当我执行查询时,我在jQuery上遇到错误parseError, Syntax Error: Invalid Character失败。

什么会导致此错误?这是我第一次做JSON查询。

var jqxhr = $.getJSON(_spPageContextInfo.webAbsoluteUrl + "/Lists/ProductList", function() {
    alert("success"); 
}).done(function() {
    alert("second success");
}).fail(function(jqxhr, textStatus, error) {
    var err = textStatus + ", " + error;
    alert("Request Failed: " + err);
}).always(function() {
    alert("complete");
});

// Perform other work here ...

// Set another completion function for the request above
jqxhr.complete(function() {
    alert( "second complete" );
});

2 个答案:

答案 0 :(得分:0)

在SharePoint网址中,_spPageContextInfo.webAbsoluteUrl + "/Lists/ProductList"引用默认列表视图页面,这意味着您没有从服务器获取JSON编码数据。

您可能需要REST端点来获取列表项,在这种情况下,URL将如下所示:

https://server/_api/lists/getbytitle('<list title>')/items

More info

示例

var listTitle = 'Tasks';  //set list title
var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('" + listTitle + "')/items";

$.getJSON(requestUrl)
.done(function(items) {
     console.log(JSON.stringify(items.value));  //print list items
}).fail(function(jqxhr, textStatus, error) {
    console.log("Request Failed: " + error);
});

答案 1 :(得分:0)

我认为jQuery getJSON函数在与sharepoint一起使用时可能会出现问题(我以为我读过最新版本的jquery并将其加入)。

我所做的是编写等效的ajax调用

var listTitle ='ProductList'; //设置列表标题

    var requestUrl = _spPageContextInfo.webAbsoluteUrl + "/_api/lists/getbytitle('" + listTitle + "')/items";               

    $.ajax({
                    url: requestUrl,
                    method: "GET",
                    headers: { "Accept": "application/json; odata=verbose" },
                    success: function (data) {
                        // Returning the results
                        alert(data);
                    },
                    error: function (data) {
                        alert(data);
                    }
         });

似乎工作得很好。