0

我正在使用 ajax 将表单发布到我的 localproxy,然后再发布到附属客户以及我的数据库。但是,它要么不使用 ajax 发布,要么出现错误。我在其他网站上使用了这种精确的代码格式,没有问题。

我的ajax代码

$(document).ready(function(){

     // ajax code start
    $('#form').on('submit', function (e){
    e.preventDefault();
    $.ajax({
    type: "POST",
    url: "/localProxy.php",
    data: $('#form').serialize(),
    success: function (response) {
            document.location = '/thank-you';
             // do something!
    },
    error: function () {
            alert('There was a problem!');  // handle error
    }
        });
        });

这是我当前的表单标题和提交代码

<form id="form" name="form" >

<input type="submit" name="submit" id="submit" value="Enter" />

默认提交激活绕过 ajax 或出现警报消息。

4

2 回答 2

0
$(document).ready(function(){

 // ajax code start
$('#form').on('submit', function (e){
   e.preventDefault();
   $.post('/localProxy.php', $('#form').serialize(),
      success: function (response) {
        document.location = '/thank-you';
         // do something!
      },
      error: function () {
         alert('There was a problem!');  // handle error
      });
   });
});

试试这个。这是 ajax 调用的简写版本。bt 我不确定这是 OK 还是 NT。

于 2012-09-27T04:49:14.193 回答
0

确保您的localProxy脚本中没有错误,或者它确实存在。我还注意到您的代码中缺少功能附件:

$(document).ready(function(){
     // ajax code start
    $('#form').on('submit', function (e){
        e.preventDefault();
        $.ajax({
            type: "POST",
            url: "/localProxy.php",
            data: $('#form').serialize(),
            success: function (response) {
                    document.location = '/thank-you';
                     // do something!
            },
            error: function () {
                    alert('There was a problem!');  // handle error
            }
        });
    }); // <--- I just added this and it's submitting properly
});
于 2012-09-27T04:52:58.947 回答