dtqf81594 2012-06-15 21:25
浏览 54
已采纳

jquery序列化,获取参数和图像

Ok, this one is weird, i have this code:

$('#nps').submit(function(e) {
    e.preventDefault();
    var images = 'pic='+$('img[name="pic"]').attr('src');
    var inputs = $(this).serialize();
    $.ajax({
      url: "pages/"+page+".php?"+inputs+'&'+images+'&action='+param,
      cache: false
    }).done(function( html ) {
        update(html);
    }).fail(function (){
        window.location = "/";
    });
});

What i'm trying to do is to pass to a php page some get parameters with the form inputs and the src of an image with the name pic.

The problam is that the pic param doesn't get passed all of the time, one time it is and the other it's not, randomally...

  • 写回答

1条回答 默认 最新

  • duanbeng6709 2012-06-15 21:30
    关注

    You are not properly encoding your request parameters. Make sure you url encode them using the encodeURIComponent function:

    $('#nps').submit(function(e) {
        e.preventDefault();
        var images = encodeURIComponent($('img[name="pic"]').attr('src'));
        var inputs = $(this).serialize();
        $.ajax({
            url: 'pages/' + page + '.php?pic=' + images + '&action=' + encodeURIComponent(param),
            type: 'POST',
            data: inputs,
            cache: false
        }).done(function( html ) {
            update(html);
        }).fail(function (){
            window.location = "/";
        });
    });
    

    Now in your php script you will be able to use $_GET['pic'] and $_GET['action'] and then $_POST['someFormElementName'] (for the form elements).

    本回答被题主选为最佳回答 , 对您是否有帮助呢?
    评论

报告相同问题?

悬赏问题

  • ¥15 对于知识的学以致用的解释
  • ¥50 三种调度算法报错 有实例
  • ¥15 关于#python#的问题,请各位专家解答!
  • ¥200 询问:python实现大地主题正反算的程序设计,有偿
  • ¥15 smptlib使用465端口发送邮件失败
  • ¥200 总是报错,能帮助用python实现程序实现高斯正反算吗?有偿
  • ¥15 对于squad数据集的基于bert模型的微调
  • ¥15 为什么我运行这个网络会出现以下报错?CRNN神经网络
  • ¥20 steam下载游戏占用内存
  • ¥15 CST保存项目时失败