ارسال (Post) اطلاعات توسط جاوا اسکریپت
یکی از مسائلی که چالش فراوانی در ساخت یکی از پروژههایم بوجود آورده، ارسال اطلاعات فقط به کمک جاوا اسکریپت بود. پس از جستجو و بررسیهای فراوان، سرانجام به کد مفیدی دست یافتم که اینجا در قالب یک تابع برای استفادهی شما قرار میدهم.
function OpenWindowWithPost(url, windowoption, name, params)
{
var form = document.createElement("form");
form.setAttribute("method", "post");
form.setAttribute("action", url);
form.setAttribute("target", name);
for (var i in params) {
if (params.hasOwnProperty(i)) {
var input = document.createElement('input');
input.type = 'hidden';
input.name = i;
input.value = params[i];
form.appendChild(input);
}
}
document.body.appendChild(form);
//note I am using a post.htm page since I did not want to make double request to the page
//it might have some Page_Load call which might screw things up.
window.open("post.htm", name, windowoption);
form.submit();
document.body.removeChild(form);
}