0

在以下情况下如何访问 PHP 文件中的序列化数据?代码和序列化数据如下:

$(document).ready(function() { $(document).on('click', '#delete_url', function (e) {    
e.preventDefault();

var items = new Array();
$("input:checked:not(#ckbCheckAll)").each(function() {
  items.push($(this).val());
});

var str = $("#user_filter").serialize();    


$.ajax({
  type: "POST",
  url: "manage_users.php?op=delete_bulk_users&items="+items,
  data: str,
  dataType: 'json',  
  success: function(data) {
    //var message       = data.success_message;            
    var redirect_link = data.href;
    alert(redirect_link);
      window.location.href = redirect_link;
  }          
});

}); });

我序列化后输入的数据str如下:

op=group_filter&page=1&from_date=11%2F10%2F2000&social_login=&to_date=11%2F10%2F2013&login_criteria=all&user_name=&user_state=&user_email_id=&user_city=

现在 PHP 文件(manage_users.php)如下:

/*The code is actually one of the switch casees*/
prepare_request();
  $request = empty( $_GET ) ? $_POST : $_GET ;
$op         = $request['op'];
switch( $op ) {
case "delete_bulk_users":
print_r($request);/*For printing the received array of values after form submission
                    Here I'm not getting serialized data */
}

提前致谢。

4

1 回答 1

0

serialize函数旨在用于从输入集合(或整个表单)中为 URL 创建查询字符串,并且不必对诸如/(表示 URL 中的目录)之类的字符进行编码。没有办法告诉.serialize()转换为“正确”格式,因为它已经是.

如果您将结果.serialize()用作 AJAX 请求的一部分,则可以这样做:

$.ajax({
    url: 'yourpage.php',
    data: str, // the string returned by calling .serialize()
    ... // other options go here
}).done(function(response) {
    // do something with the response
});

您的服务器应在收到请求时处理这些字符的解码并向您提供正确的值。

如果您将它用于其他用途,您可以尝试使用本机 JavaScriptdecodeURIComponent函数将这些编码字符转换回来,如下所示:

str = decodeURIComponent(str);

请注意,调用decodeURIComponent然后尝试将其用于 AJAX 请求不起作用。

有关 URI 编码的更多信息,请阅读 MDN 条目以获取encodeURIComponent.

于 2013-10-11T14:23:22.363 回答