2012-05-22 65 views
6

我想發送表單數據到當前頁面使用jQuery進行變量處理。當我發送POST請求時,服務器收到一個GET請求,並且我的URL更改爲匹配該GET。我明白,如果我不隱式使用type: "POST",jQuery會自動轉向GET請求,但這正是我所相信的。任何幫助,將不勝感激。jQuery的AJAX POST變成GET

<script> 
    function addadomain(){ 
     $.ajax({ 
      type: "POST", 
      url: "domains.php", 
      data: $("#domainform form").serializeArray(), 
      success: function() { 
       $("#domainlist").load("domains.php #domainlist"); 
       $.ajaxSetup({ cache: false }); 
      } 
     }); 
     return false; 
    }; 
</script> 

<a id="displayText" href="#" onclick="toggle();" >Add Domains</a> 

<div id="toggleText" style="display: none"> 
    <form name="adddomain" id="domainform" autocomplete="on"> 
     <input type="hidden" name="userid" id="userid" value="<?PHP echo $loggedInUser->user_id; ?>" /> 

     Domain: <input type="text" name="domain" id="domain" required="required" /><br /> 
     Keyword: <input type="text" name="keyword" id="keyword" required="required" /><br /> 
     Facebook Url: <input type="text" name="facebook" id="facebook" /><br /> 
     Twitter Url: <input type="text" name="twitter" id="twitter" /><br /> 
     Analytics ID#: <input type="text" name="analytics" id="analytics" /><br /> 
     Blogspot Url: <input type="text" name="blogspot" id="blogspot" /><br /> 

     <input type="submit" onclick="addadomain()" id="submit" value="submit" /> 
    </form> 
</div> 
+5

#domainform裏面沒有任何窗體... – thebjorn

回答

8

,你可能需要做:

e.preventDefault() 

http://api.jquery.com/event.preventDefault/

加載頁面時,讓你的提交犯規其默認操作上的Ajax調用上面......還有,你可以在表單標記中添加窗體動作action="post"

+1

首先,他需要將「onclick =」HTML屬性到'.on('click',function(e){...})' – Blazemonger

+2

這將是'onclick =「返回addadomain();'然後'返回false'從函數本身,因爲他沒有直接附加點擊事件的功能。 +1來識別問題。 –