2012-07-12 97 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