Last active
November 17, 2016 15:43
-
-
Save juanmaguitar/c3120fc83cf6bee13f3f564514f483c5 to your computer and use it in GitHub Desktop.
Demo jQuery POST - Express
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| const express = require('express'); | |
| const bodyParser = require('body-parser') | |
| const app = express() | |
| const PORT = 3000; | |
| app.use( express.static('public') ) | |
| // parse application/x-www-form-urlencoded | |
| app.use(bodyParser.urlencoded({ extended: false })) | |
| // parse application/json | |
| app.use(bodyParser.json()) | |
| app.post('/bands', (req, res) => { | |
| console.log(req.body) | |
| res.sendStatus(200); | |
| }) | |
| app.listen(PORT, () => console.log(`Listening on PORT ${PORT}...`) ) |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| { | |
| "name": "express-jquery-post", | |
| "version": "1.0.0", | |
| "description": "", | |
| "main": "app.js", | |
| "scripts": { | |
| "dev": "nodemon app" | |
| }, | |
| "keywords": [], | |
| "author": "", | |
| "license": "ISC", | |
| "dependencies": { | |
| "body-parser": "^1.15.2", | |
| "express": "^4.14.0" | |
| } | |
| } |
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| <!DOCTYPE html> | |
| <html lang="en"> | |
| <head> | |
| <meta charset="UTF-8"> | |
| <title>Demo jQuery POST - Express </title> | |
| </head> | |
| <body> | |
| <h1>Bands!</h1> | |
| <button id="send">Send Data!</button> | |
| <script src="https://code.jquery.com/jquery-2.0.3.min.js"></script> | |
| <script> | |
| $("#send").on('click', () => { | |
| console.log("button clicked...") | |
| var data = { | |
| name: "beatles", | |
| styles: ["rock", "blues", "pop"] | |
| } | |
| $.ajax({ | |
| url: '/bands', | |
| type: 'POST', | |
| contentType: 'application/json', | |
| data: JSON.stringify(data) | |
| }) | |
| .done(function() { | |
| console.log("Data SENT succesfully!"); | |
| }) | |
| }); | |
| </script> | |
| </body> | |
| </html> |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment