1var formData = {name:"John", surname:"Doe", age:"31"}; //Array
2
3$.ajax({
4 url : "https://example.com/rest/getData", // Url of backend (can be python, php, etc..)
5 type: "POST", // data type (can be get, post, put, delete)
6 data : formData, // data in json format
7 async : false, // enable or disable async (optional, but suggested as false if you need to populate data afterwards)
8 success: function(response, textStatus, jqXHR) {
9 console.log(response);
10 },
11 error: function (jqXHR, textStatus, errorThrown) {
12 console.log(jqXHR);
13 console.log(textStatus);
14 console.log(errorThrown);
15 }
16});
1<script type="text/javascript">
2 function send() {
3 var person = {
4 name: $("#id-name").val(),
5 address:$("#id-address").val(),
6 phone:$("#id-phone").val()
7 }
8
9 $('#target').html('sending..');
10
11 $.ajax({
12 url: '/test/PersonSubmit',
13 type: 'post',
14 dataType: 'json',
15 contentType: 'application/json',
16 success: function (data) {
17 $('#target').html(data.msg);
18 },
19 data: JSON.stringify(person)
20 });
21 }
22</script>
1var myKeyVals = { A1984 : 1, A9873 : 5, A1674 : 2, A8724 : 1, A3574 : 3, A1165 : 5 }
2
3
4
5var saveData = $.ajax({
6 type: 'POST',
7 url: "someaction.do?action=saveData",
8 data: myKeyVals,
9 dataType: "text",
10 success: function(resultData) { alert("Save Complete") }
11});
12saveData.error(function() { alert("Something went wrong"); });
1$.ajax({
2 method: "POST",
3 url: "some.php",
4 data: { name: "John", location: "Boston" }
5})
6
7
1$(function(){
2 $('#myForm').on('submit', function(e){
3 e.preventDefault();
4 $.post('http://www.somewhere.com/path/to/post',
5 $('#myForm').serialize(),
6 function(data, status, xhr){
7 // do something here with response;
8 });
9 });
10});
11
1// Variable to hold request
2var request;
3
4// Bind to the submit event of our form
5$("#foo").submit(function(event){
6
7 // Prevent default posting of form - put here to work in case of errors
8 event.preventDefault();
9
10 // Abort any pending request
11 if (request) {
12 request.abort();
13 }
14 // setup some local variables
15 var $form = $(this);
16
17 // Let's select and cache all the fields
18 var $inputs = $form.find("input, select, button, textarea");
19
20 // Serialize the data in the form
21 var serializedData = $form.serialize();
22
23 // Let's disable the inputs for the duration of the Ajax request.
24 // Note: we disable elements AFTER the form data has been serialized.
25 // Disabled form elements will not be serialized.
26 $inputs.prop("disabled", true);
27
28 // Fire off the request to /form.php
29 request = $.ajax({
30 url: "/form.php",
31 type: "post",
32 data: serializedData
33 });
34
35 // Callback handler that will be called on success
36 request.done(function (response, textStatus, jqXHR){
37 // Log a message to the console
38 console.log("Hooray, it worked!");
39 });
40
41 // Callback handler that will be called on failure
42 request.fail(function (jqXHR, textStatus, errorThrown){
43 // Log the error to the console
44 console.error(
45 "The following error occurred: "+
46 textStatus, errorThrown
47 );
48 });
49
50 // Callback handler that will be called regardless
51 // if the request failed or succeeded
52 request.always(function () {
53 // Reenable the inputs
54 $inputs.prop("disabled", false);
55 });
56
57});
58