英文:
I Can't Get my Sharepoint list through the REST API in javascript
问题
我试图将我的Sharepoint列表转换为数组,但控制台返回以下错误:
Demande.JS:66 Uncaught ReferenceError: $ is not defined
at arrayFromSharepointList (Demande.JS:66:9)
at Demande.JS:88:19
以下是我的代码:
function arrayFromSharepointList() {
var array = [];
var appWebUrl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: appWebUrl + "/_api/web/lists/getbyTitle('MyList')/items",
type: "GET",
async: false,
headers: {
"accept": "application/json;odata=verbose"
},
success: function (data) {
array = data.d.results;
},
error: function (err) {
console.log(err);
}
});
return array;
}
我参考了这个链接:如何从SharePoint列表创建JavaScript数组?
我是JavaScript的新手,但我真的需要一种将我的Sharepoint列表链接到HTML表单的方法。如果你没有解决上述问题但有一种替代的方法将HTML链接到Sharepoint,我也感兴趣。
英文:
I'm trying to get my sharepoint list into an array but the console return me this:
>Demande.JS:66 Uncaught ReferenceError: $ is not defined
> at arrayFromSharepointList (Demande.JS:66:9)
> at Demande.JS:88:19
here is my code:
function arrayFromSharepointList()
{
var array = []
var appWebUrl = _spPageContextInfo.webAbsoluteUrl;
$.ajax({
url: appWebUrl + "/_api/web/lists/getbyTitle('MyList')/items",
type: "GET",
async: false,
headers:
{
"accept": "application/json;odata=verbose"
},
success: function (data)
{
array = data.d.results;
},
error: function (err)
{
console.log(err);
}
});
return array;
}
I used this as a reference: How to create a javascript array from a SharePoint List?
I am new to Javascript but i really need a way to link my sharepoint list to a html form, if you don't have a solution for the above but have an alternative way to link html to sharepoint I'm also interested.
答案1
得分: 2
你之所以收到此错误是因为缺少必要的 jQuery 文件依赖项,这是使用 $.ajax
所必需的。
你可以在你的 HTML 文件中从 CDN 引用 jQuery 文件,如下所示:
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
或者你可以从 CDN JS 中使用它:
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
</head>
或者
如果你将此 HTML 文件添加到 SharePoint 网站页面中,并使用脚本编辑器或内容编辑器网络部件,我建议你下载 jQuery 文件,将其上传到你的 SharePoint 库中(最好是“站点资产”库),然后在 HTML 文件中以如下方式访问它:
SharePoint 网站的相对路径:
<head>
<script type="text/javascript" src="/sites/MySite/SiteAssets/jquery.min.js"></script>
</head>
SharePoint 网站的绝对路径:
<head>
<script type="text/javascript" src="https://contoso.sharepoint.com/sites/MySite/SiteAssets/jquery.min.js"></script>
</head>
英文:
You are receiving this error because you are missing dependency of jQuery file which is required to use the $.ajax
.
You can refer the jQuery file from CDN in your HTML file like below:
<head>
<script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js"></script>
</head>
Or you can use it from CDN JS like:
<head>
<script type="text/javascript" src="https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js"></script>
</head>
OR
If you are adding this HTML file on SharePoint site pages using script editor or content editor web part, I will recommend you to download the jQuery file, upload it in your SharePoint library (prefer "Site Assets" library), then access it from there in your HTML file like:
Relative path of SharePoint site:
<head>
<script type="text/javascript" src="/sites/MySite/SiteAssets/jquery.min.js"></script>
</head>
Absolute path of SharePoint site:
<head>
<script type="text/javascript" src="https://contoso.sharepoint.com/sites/MySite/SiteAssets/jquery.min.js"></script>
</head>
通过集体智慧和协作来改善编程学习和解决问题的方式。致力于成为全球开发者共同参与的知识库,让每个人都能够通过互相帮助和分享经验来进步。
评论