delete request by using FormData returning error 500

i'm working on delete an attachment by send a request with form data containing an URL through an API path with an id

deleteAttachment(id, url) {
    const formData = new FormData();
    formData.append('url', url);
    const config = {
      headers: {
        'content-type': 'multipart/form-data',
      },
    };
    return Repository.delete(`${resource}/delete-file/${id}`, formData, config);
  },

it has no problem with the id and url as i console it and return the right value. i've test with postman apps by using content-type: json/application and it's says that the request does not have multipart/form-data content-type. then i change the content-type to form-data by key in key = url and value = url and it's successfully deleted. but this deleteAttachment() function did not work and returning this error

xhr.js?b50d:172 DELETE https://anURLPath/api/employee/delete-file/38598 500

Access to XMLHttpRequest at 'https://anURLPath/api/employee/delete-file/38598' from origin 'http://localhost:8080' has been blocked by CORS policy: No 'Access-Control-Allow-Origin' header is present on the requested resource.

what's actually happen and the caused of this error? i've used this same code to post API ant it's perfectly worked. but in this delete API is returned the error. can someone help me?

Answers:

Answer

Please add Delete method in Access-Control-Allow-Methods header. POST and GET are added by default but you have to add DELETE method on server side.

"Access-Control-Allow-Methods", "GET, POST, DELETE"

Tags

Recent Questions

Top Questions

Home Tags Terms of Service Privacy Policy DMCA Contact Us

©2020 All rights reserved.