开发者

Ajax post to same page and reload the page without destroying the post parameters

Anyone knows how to preserve the post parameters after windows.location.reload(true) ?

function handler(ev开发者_Go百科ent,ui)
{
  $(this).find(".thumb").remove();
  $(this).append('<img class="thumb" src="'+ui.draggable.attr('src')+'">');

  imageurl = ui.draggable.attr('src');

  $.ajax({
     type: "POST",
     url: 'www.check.com.sg/', 
     data: {name: imageurl}, 
     complete:function(){
        window.location.reload(true);
     } 
  });
}

If i do a reload on the page. the post parameters will be destroy


First your approach is wrong for AJAX; one shouldn't refresh the page after success instead use .html() or .load() method of jQuery.

Now if still you want to do the same then it can't be done using

window.location.reload(true);

instead use

window.location.href

So

function handler(event, ui) {
    $(this).find(".thumb").remove();
    $(this).append('<img class="thumb" src="' + ui.draggable.attr('src') + '">');
    imageurl = ui.draggable.attr('src');
    $.ajax({
        type: "POST",
        url: 'www.check.com.sg/',
        data: {
            name: imageurl
        },
        complete: function() {
            var currentURL = window.location.href + '?var1=' + var1 + '&var2=' + var2;
            window.location.href = currentURL;
        }
    });
}


Yep you can do it. Just put all your post variables in a hidden form on the page like this :

<form action="" method="post" id="myform">
    <input type="hidden" name="my_var1" value="value_of_my_var1" />
    <!-- More inputs here.... -->
</form>

And then submit your form when your ajax call is done :

function handler(event,ui)
{
  $(this).find(".thumb").remove();
  $(this).append('<img class="thumb" src="'+ui.draggable.attr('src')+'">');

  imageurl = ui.draggable.attr('src');

  $.ajax({
     type: "POST",
     url: 'www.check.com.sg/', 
     data: {name: imageurl}, 
     complete:function(){
        $('#myform').submit();
     } 
  });
}
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜