javascript - JQuery.post() performs a GET? - Stack Overflow

I'm using $.post() to do the following...$.post('login',details);I would assume this POS

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 pleted 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.

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 pleted 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.

Share Improve this question edited Oct 30, 2011 at 20:03 user596186 asked Oct 30, 2011 at 19:54 user596186user596186 5891 gold badge7 silver badges16 bronze badges 3
  • Are you using Firebug or something similar to see the request headers, does it give you any errors? Is your JQuery statement properly formatted? $.post("login.php", { login: "John", pass: "XXXX" } ); – GEMI Commented Oct 30, 2011 at 20:00
  • Can you show us the resulting URL and the contents of the details string? That shouldn't be happening (and I just verified that $.post works as expected on my machine in Chrome). – ZenMaster Commented Oct 30, 2011 at 20:00
  • I've updated my question to reflect your requests. Also I'm using Chrome. – user596186 Commented Oct 30, 2011 at 20:09
Add a ment  | 

4 Answers 4

Reset to default 7

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/"!

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

If you are doing this from your home puter, chances are you don't have PHP installed on your puter. 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

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.

发布者:admin,转转请注明出处:http://www.yc00.com/questions/1744870593a4598239.html

相关推荐

  • javascript - JQuery.post() performs a GET? - Stack Overflow

    I'm using $.post() to do the following...$.post('login',details);I would assume this POS

    1天前
    20

发表回复

评论列表(0条)

  • 暂无评论

联系我们

400-800-8888

在线咨询: QQ交谈

邮件:admin@example.com

工作时间:周一至周五,9:30-18:30,节假日休息

关注微信