Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

ajax ignores url

$.ajax({
    method: 'post',
    url: '/send.php',
    data: { 'username': user }
})

It ignores the url bit and sends the parameters from data to the current page and sends me to

mywebsite.com/currentpage.php?username=user 

Instead of

mywebsite.com/send.php?username=user
like image 490
DeanR Avatar asked Dec 06 '25 01:12

DeanR


2 Answers

It would appear that what you're experiencing may be a bug, as I am unable to reproduce it.. However, assuming it is jQuery, the correct option is type, not method, as shown in the documentation of the $.ajax() method:

$.ajax({
    type:'post',
    url: '/send.php',
    data: { 'username': user }
});

EDIT 1:

.. As per the comments, it would appear you're trying to use ajax on a form.. without preventing the default action of the form, and unless I'm mistaken, when one does not supply a form method, it defaults to get.. and the action unless specified defaults to the current page.

In whatever function you are doing this in.. I'm going to assume submit() for this example, you either want to return false or e.preventDefault(), where e is the event object passed to the function in the submit event. Example:

$("form").submit(function(e) {
    e.preventDefault();
    //do your ajax here.
});
like image 55
Daedalus Avatar answered Dec 08 '25 17:12

Daedalus


I experienced this recently. On investigation, I found that the original target of the ajax request was redirecting. Chrome Dev Tools was showing the second rather than the original target of $.ajax.

like image 22
Vivek Kodira Avatar answered Dec 08 '25 16:12

Vivek Kodira



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!