我无法通过JavaScript中的REST API获取我的SharePoint列表。

huangapple go评论58阅读模式
英文:

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:

&lt;head&gt;
   &lt;script type=&quot;text/javascript&quot; src=&quot;https://ajax.googleapis.com/ajax/libs/jquery/3.6.4/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

Or you can use it from CDN JS like:

&lt;head&gt;
   &lt;script type=&quot;text/javascript&quot; src=&quot;https://cdnjs.cloudflare.com/ajax/libs/jquery/3.7.0/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

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:

&lt;head&gt;
   &lt;script type=&quot;text/javascript&quot; src=&quot;/sites/MySite/SiteAssets/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

Absolute path of SharePoint site:

&lt;head&gt;
   &lt;script type=&quot;text/javascript&quot; src=&quot;https://contoso.sharepoint.com/sites/MySite/SiteAssets/jquery.min.js&quot;&gt;&lt;/script&gt;
&lt;/head&gt;

huangapple
  • 本文由 发表于 2023年6月13日 17:15:24
  • 转载请务必保留本文链接:https://go.coder-hub.com/76463376.html
匿名

发表评论

匿名网友

:?: :razz: :sad: :evil: :!: :smile: :oops: :grin: :eek: :shock: :???: :cool: :lol: :mad: :twisted: :roll: :wink: :idea: :arrow: :neutral: :cry: :mrgreen:

确定