user1016403
user1016403

Reputation: 12621

form action in html when jquery and ajax are used?

I have below Jquery/ajax code to call a servlet

$.ajax({
        type: "POST",
        url: "./myController",
        data: dataString,
        ............

    });

i wrote a JQuery function which is called on submit click. i already mentioned url in AJax call as above ./myController.

in that case what should i mention in form action ?

<form action="./myController"

in form action do i need to mention url as above ? because i already mentioned it in ajax call above.

Thanks!

Upvotes: 0

Views: 1325

Answers (3)

Claudio Redi
Claudio Redi

Reputation: 68400

Having the url set on your ajax call is enough for your purpose, no need to change anything on form element.

As @Willem Ellis said, you'll need to prevent default behavior on using preventDefault() , otherwise your page will do a full post to server after performing your ajax call.

Also, from your comments, I recommend you to handle submit event like this

$('#theform').submit(function(e){ 
   e.preventDefault();
   ... // your ajax logic, etc
});

Upvotes: 0

Matthew Auld
Matthew Auld

Reputation: 408

You could remove the action from the actual form and add a onsubmit tag. So the form would just fire the ajax/JQuery POST.

<form onsubmit="submitform();return false;">

Upvotes: 0

Adil Shaikh
Adil Shaikh

Reputation: 44740

action url in your form is not required here. although, you can make use of your form action in your ajax call -

$('form').on('submit',function(e){
    e.preventDefault(); // to prevent default submit action
    $.ajax({
        type: "POST",
        url: this.action,
        ......
}) 

Upvotes: 1

Related Questions