Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Jquery ajax post request not working

I have a simple form submission with ajax, but it keeps giving me an error. All the error says is "error". No code, no description. No nothing, when I alert it when it fails.

Javascript with 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>

and if there are no problems there, then the server-side (pyramid):

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: After some more debugging with firebug, I discovered that the post request body contains only post.submitted=Post, instead of the intended result of {"post-form": postcontent}.

like image 855
Wiz Avatar asked Jul 21 '12 21:07

Wiz


2 Answers

According to jQuery documentation, you must declare the data type:

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

Also, looking at your server-side code, you don't actually want to post JSON formatted data. This {"post-form":postcontent} is JSON formatted data. What you actually want to do is send TEXT or HTML. Seeming as it's form data, I would guess at TEXT.

Try this:

$.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);
  }
});
like image 196
TheCarver Avatar answered Sep 18 '22 13:09

TheCarver


Since you are posting JSON-data you have to declare the dataType "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);
  }
like image 31
Besnik Avatar answered Sep 21 '22 13:09

Besnik