AJAX post不工作 [英] AJAX post not working

查看:117
本文介绍了AJAX post不工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我使用jQuery和Codeigniter通过AJAX更新数据库。下面的代码似乎没有做任何事情或发送任何东西给我的控制器,当点击按钮...

I'm using jQuery and Codeigniter to update the database via AJAX. The following code does not seem to do anything or send anything to my controller when the button is clicked...

jQuery:

$("#button_submit1").click(function(e) {

    $.ajax({
        type: "POST",
        url: window.location.href,
        dataType: "json",
        data: $('#writereview_form').serialize() + '&ajax=true',
        cache: false,
        success: function(data) {
                    alert("yay");
                    //$("#writereview_box").fadeOut(1000);
        }
    });

    return false;

});

HTML:

<form action="http://mysite.com/places/writereview/2107" id="writereview_form" method="post" accept-charset="utf-8">   

...
...

<input type="submit" name="submit" value="Submit Review" class="button_submit" id="button_submit1">

任何想要为什么不发送ajax数据?

Any ideas why the ajax data is not being sent?

推荐答案

在这种情况下,它更好地绑定到表单上的'submit'事件,然后使用 preventDefault()

In this case its better to bind to the 'submit' event on the form and then use preventDefault() to stop the HTML submission and use Ajax instead.

您还应该使用窗体的操作代替 window.location

You should also use the form's action instead of window.location

$("#writereview_form").submit(function(e) {
  var $this = $(this);
  e.preventDefault();

  $.ajax({
      type: "POST",
      url: $this.attr('action'),
      dataType: "json",
      data: $this.serialize() + '&ajax=true',
      cache: false,
      success: function(data) {
                  alert("yay");
                  //$("#writereview_box").fadeOut(1000);
      }
  });

});

这篇关于AJAX post不工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆