2012-11-28 41 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