之前是用jQuery的post()方法實作向伺服器POST資料,請求結束後還是在本頁面。
現在新需求是請求結束後需要跳轉到伺服器傳回的HTML頁面,這個類似于普通form送出後跳轉的過程。
使用表單進行POST的方法
點選Submit 就可以向"/B"POST資料并跳轉到B頁面
Ajax方法
$.post("/B",{"foo":"bar"},function(){
window.location.href="/B" target="_blank" rel="external nofollow" ;
})
這種方法能夠POST自定義資料但是雖然能夠手動跳轉到B頁面,但是并沒有辦法再傳一次參數給B頁面
是以可以用js 動态模拟一個form表單送出的操作
$.extend({
StandardPost:function(url,args){
var body = $(document.body),
form = $("
"),
input;
form.attr({"action":url});
$.each(args,function(key,value){
input = $("");
input.attr({"name":key});
input.val(value);
form.append(input);
});
form.appendTo(document.body);
form.submit();
document.body.removeChild(form[0]);
}
});