【问题标题】:Ajax is giving me an error when I send data当我发送数据时,Ajax 给我一个错误
【发布时间】:2013-01-19 17:45:50
【问题描述】:

我正在尝试通过 Ajax 提交表单,但我无法提交。我有多个表格,我正在使用 (this) 提交数据。我收到了错误From error:0 error。警报消息显示我拥有价值。

<script type="text/javascript">
$(document).ready(function() {

    $(".submitform").click(function (){
        alert ($(this).parent().serialize());
        $.ajax({
                type: "POST",
                url: "reply_business.php",
                timeout:5000,
                data: $(this).parent().serialize(),
                beforeSend: function(xhr){
                    $('#load').show();
                },
                success: function(response){
                    $(this).parent().find('.sentreply').append(response);
                    $('.sentreply div:last').fadeOut(10).fadeIn(2000);
                    //uncomment for debugging purposes
                    //alert(response);
                },
                error: function(jqXHR) {
                   alert ('From error:' + jqXHR.status + ' ' +jqXHR.statusText);
                },
                complete: function(jqXHR, textStatus){
                    //uncomment for debugging purposes
                    //alert ('From complete:' + jqXHR.status + ' ' +jqXHR.statusText + ' ' + textStatus);
                    $('#load').hide();
           }
        });

    });
}); 

    </script>

我正在通过 PHP 代码创建下面的表单

foreach ($array['business_ids'] as $business) 
                  {              

                        ?>
<form >
  <input type="hidden" name="b_id" value="<?php echo $business ; ?>" />
  <input type="hidden" name="c_id" value="<?php echo $sqlr['conversation_id']; ?>" />
  <input type="hidden" name="q_id" value="<?php echo $sqlr['query_id']; ?>" />
  <input type="hidden" name="u_id" value="<?php echo $sqlr['u_id']; ?>" />
  &nbsp;&nbsp;&nbsp;&nbsp; &nbsp; &nbsp;
  <textarea  name="reply">Type the reply here.</textarea>
  <input type="submit"  class="submitform" value="Submit">
</form>
<?php


                  }

我不明白为什么 Ajax 无法发送数据。

【问题讨论】:

  • 请求在您的网络调试器中看起来如何?服务器有响应吗?
  • 类似的问题,这甚至可以解决你的问题:stackoverflow.com/questions/5661813/…
  • @Bergi - 请求页面的状态为 CANCELED?知道为什么会这样。在那个 php 中,我刚刚看到标题写着&lt;?php header('Access-Control-Allow-Origin: *'); 这是原因吗
  • 您是否正在尝试执行 CORS 请求?
  • @doniyor : 嗯.. 但在我的情况下,我在同一个服务器上,也在同一个目录中......可能有什么问题?

标签: javascript ajax forms jquery


【解决方案1】:

没有看到标记或网络流量,我们只能猜测。也许 $(this).parent() 不是表单?

由于这个原因,附加$(form).submit() 通常比附加$(button).click() 更安全,而且$(button).click() 不会捕获按回车键提交的表单。

编辑这是一个例子:

<form id="theform">
    <input type="text" id="thetext" name="thetext" />
    <input type="submit" value="send" />
</form>
<form id="anotherform">
    <input type="text" id="anothertext" name="anothertext" />
    <input type="submit" value="save 2" />
</form>
<script>
    $(document).ready(function () {
        $("#theform").submit(function (e) {
            var data = {
                thetext: $("#thetext").val()
            };
            $.ajax("/the/server/url", {
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data: JSON.stringify(data),
                success: function (r) {
                    alert("done");
                }
            });
            // Technically you need only one or the other
            e.preventDefault();
            return false;
        });
    });
</script>

【讨论】:

  • @Robrich- 但是因为我有 10-12 个表单...如果我这样做 $(form).submit() 所有表单元素都将被提交。有什么选择吗?
  • $("#formid").submit() 只会将提交附加到这个表单。
  • 但是页面会重新加载正确。我想要 ajax 来避免页面重新加载。
  • 只要你在函数末尾return false(或e.preventDefault()),它就不会执行默认行为——在这种情况下提交表单。您可能已经为按钮点击和链接点击这样做了,以避免他们做有趣的事情。
  • 你能给我举个例子吗?我怎么能暗示它?好吧,我只想从 10 个表单中拿出我点击的那个提交到数据库中的表单,并且应该重新加载页面。
【解决方案2】:

您似乎在启动 Ajax 请求后提交了表单 - 当页面被卸载时,请求被取消。由于您的表单没有action,因此提交只会重新加载当前页面,因此您可能不会注意到它。

为防止这种情况,您需要preventDefault() 捕获的事件。此外,您不应只处理提交按钮上的 click 事件,而应处理 &lt;form&gt; 本身的 submit-事件。

【讨论】:

  • Ajax 正在$(document).ready(function() { 上加载,所以 .. 而且 `alert ($(this).parent().serialize());` 给了我正确的参数来传递。我有点困惑。那么我应该怎么做才能将表单的参数传递给Php文件。这是最简单的方法'
  • 你能给我举个例子吗?我怎么能暗示它?好吧,我只想从 10 个表单中拿出我点击的那个提交到数据库中并重新加载页面的表单?
  • @robrich : 非常感谢 :) 我花了 10 个小时来解决这个问题。我接受你的回答。但是e.preventDefault(); return false; 是如何发挥作用的呢?
  • 抱歉,我在添加示例时编辑了错误的帖子。当我一只脚走出门时,这将让我学会快速回答问题。
猜你喜欢
  • 2022-11-29
  • 1970-01-01
  • 2014-11-03
  • 2022-12-03
  • 1970-01-01
  • 2019-10-18
  • 2021-01-16
  • 2017-09-15
  • 1970-01-01
相关资源
最近更新 更多