Call delete rest api by jquery

I called the GET REST API by executing the following code:

$.getJSON('http://myapisite.com/user/1?callback=?', function(msg){ console.log(msg); }); 

But to call the DELETE REST API via jquery I tried:

 $.ajax({ url: 'http://mysite.com/user/1?callback=?', type: 'DELETE', dataType: 'json', data: '', success: function(response) { console.log('PUT completed'+response); } });. 

and this api is not being called, I want to know how I should call the DELETE REST API.

thanks

+4
source share
1 answer

You are trying to do a cross-domain query. This means that you cannot use XMLHttpRequest (AJAX framework) due to policies of the same origin . You are using a JSONP workaround that works by inserting <script> tags into your document.

Script tags always retrieve content via GET, so you cannot do DELETE, POST, PUT or anything like that with them.

The best workaround would be to have a script on your own server that proxies the DELETE request for you.

+2
source

Source: https://habr.com/ru/post/1336564/


All Articles