Advertisement
Guest User

script.js

a guest
Nov 10th, 2016
91
0
Never
Not a member of Pastebin yet? Sign Up, it unlocks many cool features!
  1. // All the code below will be run once the page content finishes loading.
  2. document.addEventListener('DOMContentLoaded', function () {
  3.     'use strict';
  4.  
  5.     // Do things when the user clicks the "Submit" button.
  6.     document.querySelector('#submit').addEventListener('click', function () {
  7.  
  8.         //output to box (fix this)
  9.         document.getElementById("url-output").innerText = 'ohayo';
  10.         //server side result: result.rows[0].original_url;
  11.     }, false);
  12.  
  13.  
  14.     console.log('welcome to jquery');
  15.     // process the form
  16.     $('outputForm').submit(function (event) {
  17.  
  18.         // get the data in the form
  19.         // there are many ways to get this data using jQuery (you can use the class or id also)
  20.         var formData = {
  21.             //element[attribute=value]
  22.             'name': $('input[name=url]').val(),
  23.         };
  24.  
  25.         // process the form
  26.         $.ajax({
  27.             type: 'POST', // define the type of HTTP verb we want to use (POST for our form)
  28.             url: '/createShorter', // the url where we want to POST
  29.             data: formData, // our data object
  30.             dataType: 'json', // what type of data do we expect back from the server
  31.             encode: true
  32.         })
  33.             // using the done promise callback
  34.             .done(function (data) {
  35.  
  36.                 // log data to the console so we can see
  37.                 console.log(data);
  38.  
  39.                 // here we will handle errors and validation messages
  40.             });
  41.  
  42.         // stop the form from submitting the normal way and refreshing the page
  43.         event.preventDefault();
  44.     });
  45. }, false);
Advertisement
Add Comment
Please, Sign In to add comment
Advertisement