2012-07-12 46 views
0

到现在为止,我使用这段代码在colorbox窗口内发布数据,直到遇到编码错误,数据被提交为“Ι§Ξ'Ξ©ΞΞ”而不是'ΔΙΧΡΩΜΟ' :用ajax在colorbox中提交表单

$("#productadd").submit(function(){ 
    $.post(
    $(this).attr('action'), 
    $(this).serialize(), 
    function(data){ 
    alert('Product added to cart!'); 
    $().colorbox.close(); 
    } 
); 

一些搜索我决定把我的代码更改为这个,所以我可以在发帖时设置编码后,但我需要一些帮助来完成它:

$.ajax({ 
    type: "POST", 
    url: $(this).attr('action'), 
    contentType: "application/json; charset=iso-8859-7", 
    dataType: "json", 
    data: "{id: '" + someId + "'}", 
    success: function(json) { 
     alert('Product added to cart!'), 
     $().colorbox.close(), 
     $("#success").html("json.length=" + json.length); 
     itemAddCallback(json); 
    }, 
    error: function (xhr, textStatus, errorThrown) { 
     $("#error").html(xhr.responseText); 
    } 
}); 

但没有内成功的呼叫:被制作,页面被重定向到表单动作中的url,而不是alert或$ ().colorbox.close()被调用,而用前面的代码,它用于在同一个窗口中提交(没有重定向到动作url)并显示警报,最后关闭colorbox窗口。 有什么建议吗?

回答

0

您必须通过阻止其执行默认操作来阻止正常表单提交,否则它将执行重定向到表单的url作为正常表单提交。

$("#productadd").submit(function(e){ 
e.preventDefault(); 
$.ajax({ 
    type: "POST", 
    url: $(this).attr('action'), 
    contentType: "application/json; charset=iso-8859-7", 
    dataType: "json", 
    data: "{id: '" + someId + "'}", 
    success: function(json) { 
     alert('Product added to cart!'), 
     $().colorbox.close(), 
     $("#success").html("json.length=" + json.length); 
     itemAddCallback(json); 
    }, 
    error: function (xhr, textStatus, errorThrown) { 
     $("#error").html(xhr.responseText); 
    } 
}); 
}); 
0

我相信这能解决您的表单提交

$("#productadd").submit(function(event){ 
event.preventDefault(); // prevent submission of the form and send ajax 
    $.post(
    $(this).attr('action'), 
    $(this).serialize(), 
    function(data){ 
    alert('Product added to cart!'); 
    $().colorbox.close(); 
    } 
); 

因为它可以依靠浏览器编码的编码,你得到这一结果,请尝试更改到其他希腊编码或UTF-8的问题。

+0

我目前的编码是iso-8859-7,所有表单都提交正常(用希腊语),除了我通过jquery/ajax登录的表单。 – bikey77 2012-07-12 10:29:28

+0

尝试UTF-8它应该工作!也看看这个,http://stackoverflow.com/questions/553463/jquery-ajax-character-encoding-problem,它为不同的编码,但它显示了一些可能的解决方案,您的编码问题 – Gntem 2012-07-12 10:36:26

+0

不幸的是,在数据库使用Windows编码,所以我需要坚持使用iso-8859-7并解决该问题。 – bikey77 2012-07-12 10:53:49