Import the necessary modules in your SPFx webpart code:
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
import { SPHttpClient, ISPHttpClientOptions, SPHttpClientResponse } from '@microsoft/sp-http'; |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
private _makePOSTRequest(): void { | |
const spOpts: ISPHttpClientOptions = { | |
body: `{ Title: 'Developer Workbench', BaseTemplate: 100 }` | |
}; | |
this.context.spHttpClient.post(`${this.context.pageContext.web.absoluteUrl}/_api/web/lists`, SPHttpClient.configurations.v1, spOpts) | |
.then((response: SPHttpClientResponse) => { | |
// Access properties of the response object. | |
console.log(`Status code: ${response.status}`); | |
console.log(`Status text: ${response.statusText}`); | |
//response.json() returns a promise so you get access to the json in the resolve callback. | |
response.json().then((responseJSON: JSON) => { | |
console.log(responseJSON); | |
}); | |
}); | |
} |
Hope this helps.
5 comments:
Hi Vardhaman, When I try to use the code. I receive CORS exception in IE for REST api ? Did you get into these issues
Hi SA,
It seems to work fine for me on IE11. Are you trying to make a request to a different domain?
Its working Fine. But not able to edit or update the list items.
Hi Vardhaman, we are trying to do OData Post query from One Site collection to another Site collection.it gives me Cross site Scripting Error but both Site collection in Same Domain.is it expected result or doing something wrong.
Hi Vinit,
It seems to be working fine for me. Here is the code I am using to create a list item in another site collection on the same domain:
https://gist.github.com/vman/0bbf85b94715895d8ff3cd19ffbfecf0
Post a Comment