user596186
user596186

Reputation: 589

JQuery.post() performs a GET?

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.

Upvotes: 4

Views: 1500

Answers (4)

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

Upvotes: 7

TheDeveloper
TheDeveloper

Reputation: 940

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

Upvotes: 1

GreyBeardedGeek
GreyBeardedGeek

Reputation: 30088

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.

Upvotes: 0

comu
comu

Reputation: 921

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

Upvotes: 0

Related Questions