简体   繁体   English

如何在 JSP 中使用带有 jQ​​uery 的 Ajax POST?

[英]How to use Ajax POST with jQuery in JSP?

This code works fine:这段代码工作正常:

$('#loginForm').attr('method', 'POST');
$('#loginForm').attr('action', basePath + url);

then I want to add a callback for handlling responsed messages and have a more common function for submission of a form, so I write a function as below:然后我想添加一个回调来处理响应消息,并有一个更常见的提交表单的功能,所以我写了一个函数如下:

//------ another function ----
saveCookie();
var loginFormParam = {
    type: "POST",
    url: basePath + "/sys/security/login.do"
};
prepareForm("loginForm", loginFormParam, loginCallback);
//----- end of another function

then,然后,

function prepareForm(whichForm, formAttr, callback) {
    $("#" + whichForm).submit(function () {
        $.ajax({
            type: formAttr.type,
            url: formAttr.url,
            success: callback
        })
    })
}

But when the form submitted, I get a GET request not POST.但是当表单提交时,我收到一个 GET 请求而不是 POST。 I must misunderstand something but I don't know where.我一定是误会了什么,但我不知道在哪里。

Thanks.谢谢。

$.ajax is default method is GET reference $.ajax是默认方法是 GET引用

Test if your formAttr contains a type and has the value of POST测试您的formAttr包含类型并具有 POST 值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM