Does jQuery.post () do a GET?

I use $.post() to do the following ...

  $.post('login',details); 

I would suggest that this is the POST data for login , but all I see is the details attached to the URL, as if it were executing a GET request on the page I am working on, and not POST to my login page. Why is this happening? My login page is intended to redirect the user to a new page after completing their login request, so I would like the POST to go to the login page so that the user can be redirected.

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

+4
source share
4 answers

POST for GET is often caused by an invalid URL.

I think your server program expects the URL to end in / and redirect to that address using HTTP 301 or 302 response; according to specifications, this means that GET should be issued instead of POST.

Try using "login /" instead; also think that your code should be called from any URL of your site; it is advisable to bind the URLs to the server root, thus, "/ login /"!

+7
source

If $ .post does not work, 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 triggers this, e.g.

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

thanks

+1
source

If you do this from your home computer, most likely PHP is not installed on your computer. This means that the browser treats the message as a normal server request. Another possibility is that you have a form that looks something like this.

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

this means that the form will be submitted without preliminary screening of jQuery, and the browser will again start looking for a document that does not exist and execute the default GET .

Or...

The file you are posting too is not called index.php or index.html, so the browser does not find anything and defaults to GET

0
source

The only thing I can think of is that if you collect the β€œdetails” on the form and use the onClick handler of the submit button of the form to invoke the ajax message, then the form can be submitted before ajax can actually be called.

You should see this using firebug or the chrome js debugger to put a breakpoint on your $ .post call to see if it hits. Or use the proven alert installation method () immediately in front of it.

0
source

All Articles