How to send a PUT/DELETE request in jQuery?

前端 未结 13 2243
别跟我提以往
别跟我提以往 2020-11-22 12:54

GET:$.get(..)

POST:$.post()..

What about PUT/DELETE?

相关标签:
13条回答
  • 2020-11-22 13:21

    You could use the ajax method:

    $.ajax({
        url: '/script.cgi',
        type: 'DELETE',
        success: function(result) {
            // Do something with the result
        }
    });
    
    0 讨论(0)
  • 2020-11-22 13:26

    ajax()

    look for param type

    Other HTTP request methods, such as PUT and DELETE, can also be used here, but they are not supported by all browsers.

    0 讨论(0)
  • 2020-11-22 13:26

    I've written a jQuery plugin that incorporates the solutions discussed here with cross-browser support:

    https://github.com/adjohnson916/jquery-methodOverride

    Check it out!

    0 讨论(0)
  • 2020-11-22 13:28

    Here's an updated ajax call for when you are using JSON with jQuery > 1.9:

    $.ajax({
        url: '/v1/object/3.json',
        method: 'DELETE',
        contentType: 'application/json',
        success: function(result) {
            // handle success
        },
        error: function(request,msg,error) {
            // handle failure
        }
    });
    
    0 讨论(0)
  • 2020-11-22 13:30

    You can do it with AJAX !

    For PUT method :

    $.ajax({
      url: 'path.php',
      type: 'PUT',
      success: function(data) {
        //play with data
      }
    });
    

    For DELETE method :

    $.ajax({
      url: 'path.php',
      type: 'DELETE',
      success: function(data) {
        //play with data
      }
    });
    
    0 讨论(0)
  • 2020-11-22 13:34

    You could include in your data hash a key called: _method with value 'delete'.

    For example:

    data = { id: 1, _method: 'delete' };
    url = '/products'
    request = $.post(url, data);
    request.done(function(res){
      alert('Yupi Yei. Your product has been deleted')
    });
    

    This will also apply for

    0 讨论(0)
提交回复
热议问题