PHP函数在"&"后不显示字符串

Pen*_*nny 5 javascript php ajax

我正在使用CI和Windows 7.在我的视图文件中,我在ajax中调用一个函数,它位于控制器中.在我的ajax中,我传递一个字符串Pack & Send,但是当我在控制器函数中回显相同的字符串时,它只是回显Pack.

我的ajax电话看起来像

<script>
      $(document).ready(function(){
        $("select#items").change(function(){
            var country_id =  $("select#items option:selected").attr('value'); 
            $("#state").html( "" );
            $("#city").html( "" );
            if (country_id.length > 0 ) { 
             $.ajax({
                    type: "POST",
                    url: "<?php echo site_url('ga_api/sample/ajax_second_dropdown');?>",
                    data: "country_id="+country_id,
                    cache: false,
                    beforeSend: function () { 
                        $('#state').html('<img src="<?php echo IMAGE_PATH;?>loader.gif" alt="" width="24" height="24">');
                    },
                    success: function(html) {    
                        $("#state").html( html );
                    }
                });
            } 
          alert(country_id);
        });
      });
    </script>
Run Code Online (Sandbox Code Playgroud)

在警报我得到整个字符串即Pack & Send.

现在我的php功能在控制器中

function ajax_second_dropdown()
{
    echo $_POST["country_id"]; 
}
Run Code Online (Sandbox Code Playgroud)

这里只是回声 Pack

我想可能是因为特殊的性格.但我想这不是场景我试图改变&到,%,',\,\& 等等,在这些情况下,我得到整个字符串.

小智 6

& 一个特殊字符:它用于查询字符串.例如,?var1=x&var2=y.

在通过HTTP发送变量之前,您需要对变量进行URL编码:

data: "country_id="+encodeURIComponent(country_id),
Run Code Online (Sandbox Code Playgroud)