2012-09-27 57 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。