这是我的 sharepoint service.ts 角度组件代码
我已将表单代码部署在我的共享点中 我想将数据发布到我的共享点列表中, 在这里,我可以从共享点列表中获取数据,但无法发布数据 在我的控制台中出现以下错误
polyfills.js:1 个帖子 https://sharepoint-base-url/_api/web/lists/getbytitle('list-name')/items 400(错误请求)
export class SharepointService {
private BaseUrl = 'sharepoint-url';
private listName = 'list-name';
private urlEncoder: HttpUrlEncodingCodec;
private userIdSource = new BehaviorSubject<string | undefined>(undefined);
userId = this.userIdSource.asObservable();
private usernameSource = new BehaviorSubject<string>('');
currentUsername = this.usernameSource.asObservable();
private projectSource = new BehaviorSubject<string>('');
currentProject = this.projectSource.asObservable();
constructor(private http: HttpClient) {
this.urlEncoder = new HttpUrlEncodingCodec();
}
changeUsername(username: string) {
this.usernameSource.next(username);
}
changeProject(project: string) {
this.projectSource.next(project);
}
getData(): Observable<any> {
const headers = new HttpHeaders({
'Content-Type': 'application/json',
Accept: 'application/json;odata=verbose',
'Access-Control-Allow-Origin': '*',
});
return this.http
.get<any>(
`${this.BaseUrl}/_api/web/lists/getbytitle('${this.listName}')/items`,
{ headers }
)
.pipe(
tap((data) => console.log('Data from SharePoint:', data)),
catchError((error: any) => {
console.error('Error fetching data from SharePoint:', error);
return throwError(error);
})
);
}
// postData(formData: any): Observable<any> {
// let url = `${this.BaseUrl}/_api/web/lists/getbytitle('${this.listName}')/items`;
// const headers = new HttpHeaders({
// 'Content-Type': 'application/json',
// 'Accept': 'application/json;odata=verbose',
// 'Access-Control-Allow-Origin': '*',
// });
// const dataToPost = {
// Title: formData.userid,
// Level: formData.level,
// Project: formData.projectClient,
// };
// console.log('Data to post:', dataToPost);
// return this.http.post<any>(url, dataToPost, { headers });
// // return this.http.post<any>(`${this.BaseUrl}/_api/web/lists/getbytitle('${this.listName}')/items?$select=enterprise ID`, { headers });
// }
// }
postData(formData: any): Observable<any> {
const url = `${this.BaseUrl}/_api/web/lists/getbytitle('${this.listName}')/items`;
const digestUrl = `${this.BaseUrl}/_api/contextinfo`;
return this.http
.post<any>(
digestUrl,
{},
{ headers: { Accept: 'application/json;odata=verbose' } }
)
.pipe(
switchMap((digestData) => {
const digestValue =
digestData.d.GetContextWebInformation.FormDigestValue;
const headers = new HttpHeaders({
'Content-Type': 'application/json',
Accept: 'application/json;odata=verbose',
'Access-Control-Allow-Origin': '*',
'X-RequestDigest': digestValue,
});
const dataToPost = {
Title: formData.username,
Project: formData.project,
};
console.log('Data to post:', dataToPost);
return this.http.post<any>(url, dataToPost, { headers });
})
);
}
}
我需要解决我的共享点控制台中的以下错误 polyfills.js:1
发布 https://sharepoint-url/_api/web/lists/getbytitle('list-name')/items 400(错误请求)
检查以下 api 来发布项目
POST https://{site_url}/_api/web/lists
Authorization: "Bearer " + accessToken
Accept: "application/json;odata=verbose"
Content-Type: "application/json"
Content-Length: {length of request body as integer}
X-RequestDigest: "{form_digest_value}"
{
"__metadata": {
"type": "SP.List"
},
"AllowContentTypes": true,
"BaseTemplate": 100,
"ContentTypesEnabled": true,
"Description": "My list description",
"Title": "Test"
}