Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

PUT Ajax request

I am new to doing Ajax request and have put together the following Pastie. Line 107 is my $.PUT and is throwing an error in firebug that $.PUT does not a function. As for the ajax request I know this is wrong however I am quite lost as to what I need to do inside the success function addCell. Am I going the right way about this?

Edited

function _ajax_request(url, data, callback, type, method) {
    return jQuery.ajax({
        type: 'PUT',
        url: "slot_days/show",
        data: data,
        success: function(data)
        {
callback($.put('/slot_days/show', { '/slot_days/': 'slot_times' }, function(result) 

            {

            });
          )
        }
    });
}

jQuery.extend({
    put: function(url, data, callback, type) {
        return _ajax_request(url, data, callback, type, 'PUT');
}});  
like image 453
Deej Avatar asked Feb 16 '12 12:02

Deej


People also ask

What is a Put request JS?

The HTTP PUT request method creates a new resource or replaces an existing resource on the server. The Content-Type request header indicates the media type of the PUT request body, and the Content-Length request header indicates the data size in the PUT request message.

What does AJAX GET request do?

AJAX stands for Asynchronous JavaScript And XML. In a nutshell, it is the use of the XMLHttpRequest object to communicate with servers. It can send and receive information in various formats, including JSON, XML, HTML, and text files.

What is AJAX POST request?

Sends an asynchronous http POST request to load data from the server. Its general form is: jQuery. post( url [, data ] [, success ] [, dataType ] ) url : is the only mandatory parameter.


1 Answers

You have an error here (the success function must be anonymous):

return 
    jQuery.ajax({
        type: 'PUT',
        url: 'slot_days/show',
        data: data,
        success: function addCell() {

        }
    });

Should be:

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

and to extend jQuery:

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

and a sample usage example:

$.put('/url', { 'foo': 'bar' }, function(result) {
    // do something with the results of the AJAX call
});
like image 116
Darin Dimitrov Avatar answered Nov 09 '22 23:11

Darin Dimitrov