CORS - תיקשורת בין client / server
CORS - Cross-Origin Resource Sharing. How to configure?
צריך להוסיף ל web.config של Web API:
- <system.webServer>
- <httpProtocol>
- <customHeaders>
- <add name="Access-Control-Allow-Origin" value="*" />
- <add name="Access-Control-Allow-Headers" value="*" />
- <add name="Access-Control-Allow-Methods" value="*" />
- </customHeaders>
- </httpProtocol>
- </system.webServer>
וב Client:
- fetch('http://localhost/WebApi/api/values', {
- method: 'POST', // or 'PUT'
- headers: {
- 'Access-Control-Allow-Origin': '*',
- 'Access-Control-Allow-Headers': '*',
- 'Content-Type': 'application/json;charset=UTF-8'
- },
- body: JSON.stringify(data),
- }).then((response) => response.json())
- .then((data) => {this.title = data;});