JavaScript How to submit form using Ajax in MVC submit form using ajax,submit form using javascript,submit form

I am getting problem to save my form data in the database. I am done small code on that which is shown below, when i enter data in form and click on my submit button it not work.

$(".btn").click(function(e) {
	e.preventDefault();

	var form = $("#frm");

	$.ajax({
		url: '/Form/Index',
		data: form.serialize(),
		type: 'POST',
		success: function(data) {
        }
	});
});
<script src="https://ajax.googleapis.com/ajax/libs/jquery/2.1.1/jquery.min.js"></script>

<form role="form" id="frm">
    <div class="form-group">
        <div class="col-sm-6 col-lg-12 col-md-12">
            <div class="form-group">
                <label for="name" style="color:black;">Product Name</label>
                <input type="text" class="form-control" id="name"
                        placeholder="Product Name" style="color:black;">
            </div>
            <div class="form-group">
                <label for="name" style="color:black;">Product Date</label>
                <input type="text" class="form-control" id="Text1"
                    placeholder="Date" style="color:black;">
            </div>
            <div class="form-group">
                <label for="name" style="color:black;">Product Price</label>
                <input type="text" class="form-control" id="Text2"
                    placeholder="Date" style="color:black;">
            </div>
        </div>
    </div>
    
    <button type="submit" class="btn btn-default" id="ok" >Submit</button>
</form>
Answer:1

As I've checked you code, client side code is working fine, The only problem I can imagine in this case is you url path.

  • make sure you are providing correct url path.

  • You should check if its hitting the that page or not.

Answer:2

Which Framework you are using. Different framework has different syntax to pass the value in URL. Check the path you are getting in the page source page view in URL parameter or you can check the error in console log after the submit. It may be not getting the correct path of your action.

Answer:3

Make sure ajax library loaded successfully, and try to have alert messages to have forward step where you reached, have this test:

$(".btn").click(function(e) {
    e.preventDefault();

    var form = $("#frm");

    $.ajax({
        url: '/Form/Index',
        data: form.serialize(),
        type: 'POST',
        success: function(data) {
        },
        beforeSend: function() {
             alert('before send alert')
        },
        error: function (request, status, error) {
             alert(error);
        },
    });
});

if beforeSend not executed so your issue is related to ajax library.

Answer:4

use this :

$("#ok").click(function(e) {
 // your code 
}

Refer to id in javascript rather than class attribute. If you refer class attribute than once it has click javascript perform preventDefault on that class so that if not refresh your page, The button is not working.

Put preventDefault function at last of your function.

Answer:5

Please help me to write a function to compute the square root of positive real numbers using the formula: x i+1 = (1/2) * (xi + (A / x1)), where 'A' - input real number. On the zero iteration next ...

Please help me to write a function to compute the square root of positive real numbers using the formula: x i+1 = (1/2) * (xi + (A / x1)), where 'A' - input real number. On the zero iteration next ...

  1. calculate square root by hand
  2. calculate square root in excel
  3. calculate square root of a number
  4. calculate square root in python
  5. calculate square root on iphone
  6. calculate square root of 2
  7. calculate square root on calculator
  8. calculate square root algorithm
  9. calculate square root in r
  10. calculate square root of 5
  11. calculate square root of 3
  12. calculate square root java
  13. calculate square root of matrix
  14. calculate square root curve
  15. calculate square root in c
  16. calculate square root of 2 by hand
  17. calculate square root without calculator
  18. calculate square root without sqrt
  19. calculate square root with exponents
  20. calculate square root of complex number

On my server side I have socket server listening and in my own laptop I have socket.io-client service and whenever I turn on both they are connecting. And when other people request to my server, ...

On my server side I have socket server listening and in my own laptop I have socket.io-client service and whenever I turn on both they are connecting. And when other people request to my server, ...

  1. error err_stream_write_after_end write after end
  2. error err_stream_write_after_end write after end node js
  3. error err_stream_write_after_end write after end express
  4. error err_stream_write_after_end write after en
  5. error err_stream_write_after_end write after end node
  6. error err_stream_write_after_end write after end createwritestream
  7. npm error err_stream_write_after_end write after end
  8. unhandledpromiserejectionwarning error err_stream_write_after_end write after end
  9. uncaught error err_stream_write_after_end write after end
  10. stack error err_stream_write_after_end write after end
  11. error code err_stream_write_after_end
  12. error code err_stream_write_after_end npm

I want to re-render my child component without re-rendering the parent component when the parent's state changes. In this example, componentWillReceiveProps is never called. Thank You! Parent ...

I want to re-render my child component without re-rendering the parent component when the parent's state changes. In this example, componentWillReceiveProps is never called. Thank You! Parent ...

  1. react render only child component
  2. react render only child
  3. react re render only child
  4. react render only one child
  5. react render only first child

For a page ("Bill4Time"), the Dashboard shows a table for displaying time entries and entering a new time entry but several control elements do not have ID's associated with them. Interestingly ...

For a page ("Bill4Time"), the Dashboard shows a table for displaying time entries and entering a new time entry but several control elements do not have ID's associated with them. Interestingly ...