douti6740 2015-12-29 08:28
浏览 13

使用参数中的数组进行ajax查询

I'm trying to make an Ajax query with an array in the parameters. Here's an example :

var info = [ter,ter,ter];

$('body').on('click','#upload', function(){
    $.ajax({
        url:'/app_dev.php/uploader_photos',
        data:{
            links : info,
        },
        dataType : 'json',
        beforeSend : function(){
            $('#upload_photo').empty();
            $('#upload_photo').append('<i class="fa fa-refresh fa-spin"></i> Loading');
        },
        success : function(data){
            $('#upload_photo').empty();
            $('#upload_photo').append('<i class="fa fa-thumbs-o-up"></i> OK');
        }
    });
});

The issue is my url is encoded this way :

/upload?links%5B%5D=ter&lioks%5B%5D=ter&lioks%5B%5D=ter

What i need to know how to encode it this way :

/upload?links=[ter,ter,ter]

Thanks for the help !

  • 写回答

3条回答 默认 最新

  • doudieheng5322 2015-12-29 08:32
    关注

    That shouldn't be a problem actually. As you have not set the Content-Type so by default jQuery uses formurlencoded query string so it encodes the values that way and it sends it as a form data instead of Request Payload.

    You can get it like:

    if(isset($_GET['links'])){
      echo $_GET['links'];
    }
    

    From the docs:

    contentType (default: 'application/x-www-form-urlencoded; charset=UTF-8') Type: Boolean or String
    When sending data to the server, use this content type. Default is "application/x-www-form-urlencoded; charset=UTF-8", which is fine for most cases. If you explicitly pass in a content-type to $.ajax(), then it is always sent to the server (even if no data is sent). As of jQuery 1.6 you can pass false to tell jQuery to not set any content type header. Note: The W3C XMLHttpRequest specification dictates that the charset is always UTF-8; specifying another charset will not force the browser to change the encoding. Note: For cross-domain requests, setting the content type to anything other than application/x-www-form-urlencoded, multipart/form-data, or text/plain will trigger the browser to send a preflight OPTIONS request to the server.

    评论

报告相同问题?

悬赏问题

  • ¥15 求帮我调试一下freefem代码
  • ¥15 R语言Rstudio突然无法启动
  • ¥15 关于#matlab#的问题:提取2个图像的变量作为另外一个图像像元的移动量,计算新的位置创建新的图像并提取第二个图像的变量到新的图像
  • ¥15 改算法,照着压缩包里边,参考其他代码封装的格式 写到main函数里
  • ¥15 用windows做服务的同志有吗
  • ¥60 求一个简单的网页(标签-安全|关键词-上传)
  • ¥35 lstm时间序列共享单车预测,loss值优化,参数优化算法
  • ¥15 Python中的request,如何使用ssr节点,通过代理requests网页。本人在泰国,需要用大陆ip才能玩网页游戏,合法合规。
  • ¥100 为什么这个恒流源电路不能恒流?
  • ¥15 有偿求跨组件数据流路径图