使用SPFx创建了一个webpart,其中使用pnpjs访问列表,在online workbench中一切正常,因为workbench的url是https://contoso.sharepoint.com/sites/testweb/_layouts/15/workbench.aspx
代码如下,这里使用sp这个全局对象,获取当前web,然后读取list数据:
sp.web.lists
.getByTitle('TestList').items.get()
.then(items => console.log(items))
.catch(err => console.log(err));
但是将webpart部署到tenant租户中,然后添加到页面上的时候,发现pnpjs读取列表数据报错,出现了一个404的错误:
Uncaught (in promise) Error: Error making HttpClient request in queryable [404]
经过debug发现pnpjs调用的rest api url是错误的:
https://contoso.sharepoint.com/sites/Testweb/SitePages/_api/web/lists/getByTitle('TestList')
正常的rest api url 应该是:
https://contoso.sharepoint.com/sites/Testweb/_api/web/lists/getByTitle('TestList')
原因是直接在页面中运行pnpjs,