4

I had called the GET REST API by following code :

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

But for calling DELETE REST API through jquery what i had tried is :

$.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 should i call the DELETE REST API .

Thanks

1 Answer 1

2

You are trying to do a cross-domain request. This means that you cannot use XMLHttpRequest (the basis of AJAX) because of the same-origin policy. You are using a workaround called JSONP, which works by inserting <script> tags into your document.

Script tags will always fetch content via GET, so you can't do a DELETE, POST, PUT or suchlike with them.

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

Sign up to request clarification or add additional context in comments.

8 Comments

FWIW, most REST APIs provide some way of using the DELETE and PUT methods using only GET and POST, since browsers' support for the DELETE and PUT methods are somewhat limited. Usually this means appending a query string (GET variables) to the url.
@all m using Zend Framework for API script. so How i can append in GET variable ?
@Rahul Are you trying to do a cross-domain request?
Yeah because the Zend Framework need to give different Domain For API Call. I think I may be wrong .
I'm sorry I can't see any evidence that the OP is facing a cross-domain issue. He's simply asking why jQuery's AJAX code is not supporting his DELETE command. I have exactly the same question. Can my jQuery front end fire off a DELETE request to '/mystuff/1' to trigger my API's delete command or do I have to fudge it with a POST?
|

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.