2012-11-23 21 views
0

我想通過查詢字符串/變量在URL中發送序列化的數據。正如你知道當我們在js中進行序列化時,它自己構建了一個查詢字符串。我將這些數據發送到django寫的服務器端。我如何做到這一點或如何收集django代碼中的數據。從js發送到變量的服務器端的序列化數據

這就是我正在做的。

selected = $('input:checkbox:checked').serialize(); 

這給我結果爲multiselect_select_month=10&multiselect_select_month=11&multiselect_select_month=05

我想給這與其他變量的URL,並收集在一個單一的可變孔串(multiselect_select_month=10&multiselect_select_month=11&)。 喜歡的東西

serialized = 'multiselect_select_month=10&multiselect_select_month=11' 

在服務器端我寫serialized = request.GET.get('serialized', '')

我怎麼能發送序列化的字符串(這是一個查詢字符串)的一個變量,這樣我可以趕上在服務器端。

注意:我想用上面的序列化數據發送其他變量。

+1

那麼,究竟是你的題? – Cerbrus

+0

我的問題是我如何發送一個單一的變量序列化的字符串(這是一個查詢字符串),以便我可以在服務器端捕獲。 – sandeep

+0

只需將它作爲參數附加到您的HTTP GET/link /然而您正在聯繫服務器:'「htpp://some.request.com/?」 + serialized' – Cerbrus

回答

0

你需要URL編碼serialized,那麼你可以發送它在一個單一的變量。

serialized = encodeURIComponent(serialized); 
var link = "http://host.com/?data=" + serialized; 
+0

這適用於我。謝謝感謝wutz。 – sandeep

0

該代碼將所有multiselect_select_month值連接到1個參數中。
(有顯著減少參數長度增加的獎金)

var multiSelects = []; 
$('input:checkbox:checked').each(function(){ 
    multiSelects.push(this.value); 
}); 
var parameter = "?multiselect_select_month=" + multiSelects.join(','); 
//Will return "?multiselect_select_month=10,11,05" instead of "multiselect_select_month=10&multiselect_select_month=11&multiselect_select_month=05" 

然後,您可以得到ultiselect_select_month參數服務器端,並explode','

string.split('10,11,05', ',') 
相關問題