javascript POST方式提交表单

来源:互联网 发布:勇者之塔龙神加护数据 编辑:程序博客网 时间:2024/05/21 17:40
<a href="javascript:;" onclick="update_submit('/hotel/datalist/', {'typeid':2});">更多></a>


/*
 * javascript POST方式提交表单
 */
function update_submit(path, params, method) {
method = method || "post";
    //如果没有特别声明,默认post方式
    var form = document.createElement("form");
    form.setAttribute("method", method);
    form.setAttribute("action", path);


    for(var key in params) {
        var hiddenField = document.createElement("input");
        hiddenField.setAttribute("type", "hidden");
        hiddenField.setAttribute("name", key);
        hiddenField.setAttribute("value", params[key]);


        form.appendChild(hiddenField);
    }


    document.body.appendChild(form); 
    form.submit();
}
</script>


Javascript Post Request like a Form Submit举例: 
       post_to_url('${pageContext.request.contextPath}/login.do', {'password':pwd, 'username':un});