2012-09-27 60 views
0

我使用ajax将表单发布到我的localproxy,然后发布到附属客户端和我的数据库。然而,它不会使用ajax发布或提出错误。我在其他网站上使用这种确切的代码格式没有问题。Ajax Post疑难解答

我的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或警报消息出现。

回答

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 
}); 
+0

awesome,修复了这个问题,谢谢 – user1684086

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确定这是好的或NT。