2012-11-28 44 views
1

我正在一個網站上工作,我正在更新D & D字符的信息。在這個特定的頁面上有可編輯的div,在其中我通過(非常不一致的)ajax POST腳本進行更改和提交。

不知何故POST不會提交任何更改,它可能是完全隨機的。有時它可以工作並更新數據庫,有時它不會。當然,這是不可能爲你們做一個POST到我的服務器,但阿賈克斯如下:這裏

<script type="text/javascript"> 
$(document).ready(function() { 
     $("#actor_result").load('xml/actortoxml.php?owner=cederman&id=2'); 
     $('#save_editable').click(function() { 
     var name = $(document).find('#actorname').text(); 
     var current = $(document).find('#currenthealth').text(); 
     var max = $(document).find('#maxhealth').text(); 
     var effects = $(document).find('#actoreffects').text(); 
     var notes = $(document).find('#actornotes').text(); 
     $.ajax({ 
      url: 'actor_profile.php', 
      type: 'POST', 
      data: { 
       update: 'true', 
       actorid: 2, 
       actorname: name, currenthealth: current, maxhealth: max, actoreffects: effects, actornotes: notes 
      }, 
     }); 
     window.location = 'actor_profile.php?id=2' 
    }); 

/* 
    var refreshId = setInterval(function() { 
     $("#actor_result").load('xml/actortoxml.php?owner=cederman&id=2'); 
    }, 300000); 
    $.ajaxSetup({ cache: false }); 
*/ 
}); 
</script>​ 

檢查搗鼓說明: http://jsfiddle.net/CgLmW/2/

+1

您是否嘗試過沒有window.location行? –

+0

嗯,我現在試過了,它似乎修復它,奇怪=/ – Marty

+0

它可以自動保存嗎? – Marty

回答

1

由於$.ajax是異步的,你要重定向在ajax請求完成之前離開頁面。這個重定向導致ajax請求在成功完成之前被殺死。

將您的重定向語句移至成功回調。

$.ajax({ 
    url: 'actor_profile.php', 
    type: 'POST', 
    data: { 
     update: 'true', 
     actorid: 2, 
     actorname: name, currenthealth: current, maxhealth: max, actoreffects: effects, actornotes: notes 
    }, 
    success : function() { 
     // gets called when ajax request completes successfully 
     window.location = 'actor_profile.php?id=2';  
    }, 
    error : function(err) { 
     // in case of error 
     console.log(err); 
     alert('error'); 
    } 
}); 
+0

謝謝你,完美!雖然我注意到了,但我如何在輸入中添加對不常用字符的支持?我是瑞典人,想用角色扮演åäö等(可能擴展到大多數角色)。 – Marty

+0

編碼是一個棘手的野獸(或至少我認爲是這樣)。你需要確保你的數據庫,服務器等都運行相同的編碼。我建議用更多specefics創建一個新的問題。 – xbonez