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).

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

报告相同问题?