得到:$ . GET (. .)

职位:$ . POST () . .

PUT/DELETE呢?


当前回答

我写了一个jQuery插件,它包含了这里讨论的跨浏览器支持的解决方案:

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

点击这里查看详情!

其他回答

我们可以扩展jQuery来创建PUT和DELETE的快捷方式:

jQuery.each( [ "put", "delete" ], function( i, method ) {
  jQuery[ method ] = function( url, data, callback, type ) {
    if ( jQuery.isFunction( data ) ) {
      type = type || callback;
      callback = data;
      data = undefined;
    }

    return jQuery.ajax({
      url: url,
      type: method,
      dataType: type,
      data: data,
      success: callback
    });
  };
});

现在你可以使用:

$.put('http://stackoverflow.com/posts/22786755/edit', {text:'new text'}, function(result){
   console.log(result);
})

从这里复制

从这里,你可以这样做:

/* Extend jQuery with functions for PUT and DELETE requests. */

function _ajax_request(url, data, callback, type, method) {
    if (jQuery.isFunction(data)) {
        callback = data;
        data = {};
    }
    return jQuery.ajax({
        type: method,
        url: url,
        data: data,
        success: callback,
        dataType: type
        });
}

jQuery.extend({
    put: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'PUT');
    },
    delete_: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'DELETE');
    }
});

它基本上只是一个复制的$.post()与方法参数调整。

这是一个更新的ajax调用,当你使用JSON与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
    }
});

我写了一个jQuery插件,它包含了这里讨论的跨浏览器支持的解决方案:

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

点击这里查看详情!

你应该会使用jQuery。ajax:

使用HTTP加载远程页面 请求。

你可以通过type选项指定应该使用哪个方法:

要发出的请求类型(“POST”或 "GET"),默认为"GET"。注:其他 HTTP请求方法,如PUT和 DELETE也可以用在这里,但是 他们并没有得到所有人的支持 浏览器。