2012-02-16 17 views
7

Sono nuovo alla richiesta Ajax e ho creato il seguente Pastie. La riga 107 è il mio $ .PUT e lancia un errore in firebug che $ .PUT non ha una funzione. Per quanto riguarda la richiesta Ajax, so che questo è sbagliato, ma sono abbastanza perso su ciò che devo fare all'interno della funzione di successo addCell. Sto andando nel modo giusto a riguardo?PUT Richiesta Ajax

cura

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'); 
}}); 
+0

Cosa è richiesto PUT o POST? –

risposta

15

Hai un errore qui (la funzione success deve essere anonimo):

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

     } 
    }); 

dovrebbe essere:

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

e di estendere jQuery:

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

e un esempio di utilizzo di esempio:

$.put('/url', { 'foo': 'bar' }, function(result) { 
    // do something with the results of the AJAX call 
}); 
+0

@Djj, no, volevo dire che hai javascript non valido là fuori. Il problema non è che sia vuoto. Lo vedremo più tardi. Il problema è che nel tuo codice hai scritto: 'success: function addCell() {}' che non è valido. Deve essere 'success: function() {}'. E se volessi invocare qualche funzione che è stata passata come parametro, ad esempio potresti: 'successo: callback'. –

+0

@Djj, certo che puoi. Passa semplicemente come terzo argomento del metodo '$ .put':' $ .put ("/" + getDiaryId() + "/ slot_days /" + getSlotId + "/ slot_times /" + 0, slot_time, addCell); ' –

+0

In questo modo: 'successo: addCell ($. Put ("/"+ getDiaryId() +"/slot_days/"+ getSlotId +"/slot_times/"+ 0, slot_time, addCell)) ' – David

0

sembra che tu non sei compreso il _ajax_request_PUT.js nel file principale. Questo è il motivo per cui non è possibile trovare la funzione $.put. Includi prima, quindi il tuo errore sparirà.

+0

destro va bene, questo può sembrare stupido, ma come faccio a fare questo? – David

+0

Proprio come si include jQuery.' " ' –

+0

Ho incluso il file nel mio file principale che continua a ricevere lo stesso errore. Probabilmente è qualcosa che ha a che fare con la mia funzione di successo e la callback – David

0

Vedere la risposta semplice e pulita qui: https://stackoverflow.com/a/11549679/916632 Basta cambiare il valore del tipo su "PUT".

Gestisce anche la risposta 415 che potrebbe causare problemi agli altri (mi ha bloccato). Anche la risposta 400 in termini di oggetto dati inviati.