我使用ajax如下:

$('.province').on('click', function (e)
{
    var optionSelected = $("option:selected", this);
    var valueSelected = this.value;
    var valueSelected = valueSelected.replace(/ /gi,"%20");
    var valueSelected = encodeURIComponent(valueSelected);
    //alert(valueSelected);
   $.ajax({
            type: 'post',
            encoding:"UTF-8",
            url: "<?php echo base_url();?>Search/cities_of_province/"+valueSelected,
            data: '',
            contentType: "charset=utf-8",
            success: function (result) {
            //alert(result);
            $('.city').html(result);

            return false;
        }
      });
    return false;
});

上面url中选择的valueselected是一个persion语句,其中包含空格。例如,它是。
当它被发送到url时,只接收第一部分(___)。
我也删除了valueSelected.replace(/ /gi,"%20")encodeURIComponent(valueSelected),但什么都没有发生。
解决办法是什么?

最佳答案

我没有遇到那样的问题。我用了noencodeURIComponentnoencoding:"UTF-8"nocontentType: "charset=utf-8"
什么都不需要。它的工作非常完美。我用下面的代码测试了它
我有HTML

<input id='yourInputId' value='استان آذربایجان شرقی' />

javascript
<script>
var valueSelected = $('#yourInputId').val();
//before ajax request
alert(valueSelected ); // it gives me here =>استان آذربایجان شرقی
//before making ajax reuest plz confirm you get above value correctly here
alert(<?php echo base_url();?>); //it must be valid as well
        $.ajax
        ({
            type: "POST",
            url: "<?php echo base_url();?>Search/cities_of_province", //should be valid
            data: { province : valueSelected },
            success: function (result) {
                alert(result); //it gives => استان آذربایجان شرقی
            },
            error:function(a)
            {
                alert(a.responseText);
            }
        });
</script>

菲律宾比索
<?php
if(isset($_POST['province']))
   $v = $_POST['province'];
else
   $v = 'Province value not provided from client side';
echo $v;

?>

09-16 04:11