2012-07-21 32 views
6

Ho una semplice sottomissione di modulo con ajax, ma continua a darmi un errore. Tutto ciò che l'errore dice è "errore". Nessun codice, nessuna descrizione. No, quando lo avverto quando fallisce.Jquery ajax richiesta post non funzionante

JavaScript con jQuery:

$(document).ready(function(){ 

     $(".post-input").submit(function(){ 
      var postcontent = $(".post-form").val(); 

      if (postcontent == ""){ 
       return false; 
      } 

      $(".post-form").attr("disabled", "disabled"); 

      $.ajax({ 
       url: '/post', 
       type: 'POST', 
       data: {"post-form": postcontent}, 
       dataType: json, 
       success: function(response, textStatus, jqXHR) { 
        alert("Yay!"); 
       }, 
       error: function(jqXHR, textStatus, errorThrown){ 
        alert(textStatus, errorThrown); 
       } 
      }); 
     }); 
    }); 

HTML:

<form class="post-input" action="" method="post" accept-charset="utf-8"> 
       <textarea class="post-form" name="post-form" rows="1" cols="10" onFocus="this.value='';return false;">What are you thinking about...</textarea> 
       <p><input class="post-submit" type="submit" name = "post.submitted" value="Post"></p> 
      </form> 

e se non ci sono problemi lì, poi il lato server (piramide):

def post(request): 
    session = Session() 
    user = authenticated_userid(request) 
    postContent = request.POST['post-form'] 
    if not postContent == '': 
     session.add(Activity(user.user_id, 0, postContent, None, None)) 
     return {} 
    return HTTPNotFound() 

UPDATE: Dopo un po 'più di debug con firebug, ho scoperto che il corpo della richiesta post contiene solo post.submitted = Posta, invece del risultato previsto di {"post-form": postcontent}.

+0

Si tratta di un errore di javascript (client), errore del server o errore python? – Hamish

+0

@Hamish attraverso il mio debug, presumo che si tratti di un errore del client. – Wiz

risposta

14

Secondo jQuery documentazione, è necessario dichiarare il tipo di dati:

$.ajax({ 
    type: 'POST', 
    url: url, 
    data: data, 
    success: success, 
    dataType: dataType 
}); 

Inoltre, guardando il tuo codice lato server, in realtà non vogliono inviare dati JSON formattato. Questo {"post-form":postcontent} è in formato JSON. Quello che in realtà vuoi fare è inviare TEXT o HTML. Sembrando come i dati del modulo, direi a TEXT.

Prova questo:

$.ajax({ 
    url: '/post', 
    type: 'POST', 
    data: 'post-form='+postcontent, 
    dataType: 'text', 
    success: function(response, textStatus, jqXHR) { 
    alert("Yay!"); 
    }, 
    error: function(jqXHR, textStatus, errorThrown){ 
    alert(textStatus, errorThrown); 
    } 
}); 
+0

+1 Questo dovrebbe farlo – ShadowStorm

+1

purtroppo, questo non ha funzionato, ma dopo un po 'più di debug con firebug, ho scoperto che il post di posta è in realtà post. submitted = Post, invece del post-form previsto = postcontent. – Wiz

+0

In realtà vuoi pubblicare JSON o solo dati di moduli regolari? – TheCarver

1

Penso che il problema è che i dati che si stanno passando non sono stati scritti correttamente.

Try to change data: {"post-form": postcontent}, 
To: 
data: 'post-form='+ $('.post-form').val(), 
3

Dal momento che stai scrivendo JSON -data si deve dichiarare il tipo di dati "JSON":

$.ajax({ 
    url: '/post', 
    type: 'POST', 
    dataType: "json", 
    data: {"post-form": postcontent}, 
    success: function(response, textStatus, jqXHR) { 
    alert("Yay!"); 
    }, 
    error: function(jqXHR, textStatus, errorThrown){ 
    alert(textStatus, errorThrown); 
    }