1

I've been trying to do a 'DELETE' call on an API for a while and it never worked. I can access the URI and see the JSON. And I've tried other web services which properly make the DELETE call. On my local machine and on Heroku, I can't get it to work. Any idea what's wrong here?

I've tried adding headers and such.

$.ajax({
      url : url,
      type : 'DELETE',
      beforeSend : function(xhr) {
            xhr.setRequestHeader("Content-Type", "text/html");
            xhr.setRequestHeader("Accept", "*/*");
          },
      crossDomain: true,
      success : function(data) {
        console.log("gone");
      },
      error : function(){
        console.log("ohcrap");
      }
    });
  • The jQuery website states for .ajax that for "type: DELETE, can also be used here, but they are not supported by all browsers." What browser are you using? – Barry Tormey Dec 18 '13 at 22:16
  • What are the headers that it returns to you in the 404. You setting crossDomain on your end doesn't matter one bit if that url's server doesn't expose a valid CORS header for you on the preflight options for DELETES. – Nick Sharp Dec 18 '13 at 22:16
  • By accessing the URI, would imply the route is a GET not a DELETE route. Are you developing the routes as well? the 404 would validate that most likely there is no DELETE route set up, but the GET route is. Do you have the backend code? – Kelly J Andrews Dec 18 '13 at 22:19
  • @NickSharp There may still be an issue with cross domain here, but it wouldn't produce a the 404. – Kelly J Andrews Dec 18 '13 at 22:20
  • @BarryTormey I'm using the newest Chrome. – user3117043 Dec 18 '13 at 22:21
  • @KellyJAndrews The backend works fine. I tested it with site such as www.hurl.it and it deleted the entry. – user3117043 Dec 18 '13 at 22:22
  • Do you have any kind of console log info you can post? Response headers, anything like that? – Kelly J Andrews Dec 18 '13 at 23:51
  • @KellyJAndrews I found out that it's sending a preflight request that's messing it up. The OPTIONS request is getting 404. Does mean sites like Hurl.it skip the preflight? – user3117043 Dec 18 '13 at 23:55
  • Does your route require `xhr.setRequestHeader("Content-Type", "text/html");` or should it be `xhr.setRequestHeader("Content-Type", "application/json");` ? – Kelly J Andrews Dec 19 '13 at 00:10
  • @KellyJAndrews Yup I tried that. It's suppose to skip the OPTIONS preflight but doesn't work. Weird thing is when I requested through ruby it works fine. – user3117043 Dec 19 '13 at 00:19
  • I think this applies to your situation - http://stackoverflow.com/questions/1099787/jquery-ajax-post-sending-options-as-request-method-in-firefox – Kelly J Andrews Dec 19 '13 at 01:45
  • The OPTIONS preflight is part of the browsers way of CORS checking... so if your server is 404'ing that it won't even be trying the POST/PUT/DELETE etc... the OPTIONS is to get the CORS header back to know if the browser is gonna let you make the DELETE/POST/PUT etc. – Nick Sharp Dec 19 '13 at 04:43
  • You can use JSONP to do that – Manish Jangir Dec 19 '13 at 05:06
  • Hey I figured it out. It turned out to be CORS problem from the server. It didn't have preflight setup so Ajax wasn't allowed to make the request, but anything from the server was fine. – user3117043 Dec 19 '13 at 06:48

1 Answers1

0

It turned out to be CORS problem from the server. It didn't have preflight setup so Ajax wasn't allowed to make the request, but anything from the server was fine.