JQuery.post() 执行 GET?

发布于 2024-12-13 07:38:57 字数 496 浏览 1 评论 0原文

我正在使用 $.post() 执行以下操作...

    $.post('login',details);

我假设此 POST 是要登录 的数据,但我所能看到的只是附加的详细信息到 url,就好像它正在对我所在的页面执行 GET 请求,而不是对我的 login 页面执行 POST 请求。为什么会出现这种情况呢?我的 login 页面的目的是在用户的登录请求完成后将用户重定向到新页面,因此理想情况下,我希望 POST 转到 login 页面,以便用户可以被重定向。

details 内容为 { username: "username", password: "password"}login 页面为 login.java使用 Jersey 的 页面。

I'm using $.post() to do the following...

    $.post('login',details);

I would assume this POST's the data to login but all I can see happening is the details being attached to the url as if it's doing a GET request to the page I'm on rather than a POST to my login page. Why would this be happening? My login page is meant to redirect the user to a new page once their login request has completed so ideally I'd like the POST to go to the login page so that the user can be redirected.

The details contents are { username: "username", pasword: "password"} and the login page is a login.java page which is using Jersey.

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

月棠 2024-12-20 07:38:57

POST 到 GET 通常是由于您使用了错误的 URL 造成的。

我认为您的服务器端程序期望 URL 以 / 结尾,并使用 HTTP 301 或 302 响应重定向到该地址;根据规范,这意味着必须发出 GET 而不是 POST。

尝试使用“login/”代替;还要考虑您的代码应该可以从您网站上的任何 url 调用;最好将 URL 锚定到服务器根目录,即“/login/”!

POST to GET is often caused by you using a wrong URL.

I think your serverside program is expecting that the URL ends in / and redirects to that address using HTTP 301 or 302 response; according to specs this means that GET must be issued instead of POST.

Try using "login/" instead; also consider that your code should be callable from any url on your site; preferably anchor the urls to server root, thus "/login/"!

尐偏执 2024-12-20 07:38:57

如果 $.post 不起作用,请尝试 $.ajax 函数:

    $('#target').click(function() {

    var formdata = $('#loginFrm').serialize(); // will produce { username: "username", pasword: "password"}
        $.ajax({type: "POST",
                        url: "urlto/tojavapage",
                        data: formdata ,
                        success: function(data) {
                            // div container to display error msg       
                            $('#loginmessage').html(data);
                        }
                    });

                    return false;


});`

确保您的提交按钮有一个调用此按钮的点击事件,例如

<input type="button" value="login" id="target" /> <div id="loginmessage"> </div>

谢谢

If $.post don't works, try the $.ajax function instead:

    $('#target').click(function() {

    var formdata = $('#loginFrm').serialize(); // will produce { username: "username", pasword: "password"}
        $.ajax({type: "POST",
                        url: "urlto/tojavapage",
                        data: formdata ,
                        success: function(data) {
                            // div container to display error msg       
                            $('#loginmessage').html(data);
                        }
                    });

                    return false;


});`

make sure your submit button has a click event that call this e.g

<input type="button" value="login" id="target" /> <div id="loginmessage"> </div>

thanks

梦巷 2024-12-20 07:38:57

如果您在家用计算机上执行此操作,则您的计算机上很可能没有安装 PHP。这意味着浏览器将帖子视为正常的服务器请求。另一种可能性是你有一个看起来像这样的表单,

<form name="xxx" method="post" action="login">
<!--Stuff-->
<input type="submit" value="xxx" />
</form>

这意味着表单将在没有首先被 jQuery 筛选的情况下提交,浏览器将再次开始寻找不存在的文档并执行默认的 GET 请求。

或者...

您正在POSTing的文件也不名为index.php或index.html,因此浏览器找不到任何内容,并且再次默认为GET

If you are doing this from your home computer, chances are you don't have PHP installed on your computer. This means that the browser treats the post as a normal server request. The other possibility is that you have a form looks something like this

<form name="xxx" method="post" action="login">
<!--Stuff-->
<input type="submit" value="xxx" />
</form>

this means that the form is going to be submitted without first being screened by jQuery and the browser will once again start looking for a documennt that is not there and do a default GET request.

Or...

The file you are POSTing too is not called index.php or index.html, so the browser finds nothing, and again, defaults to GET

偏闹i 2024-12-20 07:38:57

我唯一能想到的是,如果您正在收集表单中的“详细信息”,并使用表单提交按钮的 onClick 处理程序来调用 ajax post,
那么表单可能会在 ajax 实际被调用之前被提交。

您应该能够通过使用 firebug 或 chrome js 调试器在 $.post 调用上放置一个断点来查看它是否被击中。或者使用经过验证的方法,在其前面放置一个alert()。

The only thing that I can think of is that if you are collecting the 'details' in a form, and using the onClick handler of the form's submit button to invoke the ajax post,
then the form may be getting submitted before the ajax can actually be invoked.

You should be able to see this by using firebug or the chrome js debugger to put a breakpoint on your $.post call, to see if it gets hit. Or use the tried-and-true method of putting an alert() just before it.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文