Home > Article > Web Front-end > ajax sends nodejs request
Note: This article will introduce how to use ajax to send nodejs requests from the front-end perspective. If you are not familiar with nodejs, you can learn about it first.
With the continuous development of Web development, data interaction between the front end and the back end has become one of the important functions of many websites or applications. As an important part of front-end technology, Ajax can realize asynchronous requests and data interaction without refreshing the page. As a popular choice for back-end technology, Node.js also provides convenient APIs for handling HTTP requests and responses.
So, how to use Ajax to send Node.js requests? The following will be introduced from the following aspects.
First, you need to install Node.js and create a Node.js service. The installation process of Node.js will not be introduced here, assuming that it has been completed.
Then, enter the folder where the project is located on the command line and run the following command:
$ npm init -y $ npm install express --save
The above command will initialize a Node.js project and install the Express framework. Next, create a file named app.js and add the following code in it:
const express = require('express'); const app = express(); app.get('/api', (req, res) => { res.json({"message":"Hello World!"}); }); app.listen(3000, () => { console.log('Listening on port 3000'); });
The above code creates a route named /api, which will return a message containing "Hello World!" JSON object. The Node.js service will listen to port 3000 after it is started.
Run the following command to start the Node.js service:
$ node app.js
Visit http://localhost:3000/api in the browser, you should see the following content:
{"message":"Hello World!"}
This shows that the Node.js service has been successfully set up and can handle HTTP requests.
Next, use Ajax to send requests to the Node.js service in the front-end page. Taking jQuery as an example here, you first need to introduce the jQuery library into the html file. You can use CDN to introduce it, or you can directly download the jQuery file locally.
Add the following code in the html file:
<!DOCTYPE html> <html> <head> <script src="https://cdn.jsdelivr.net/npm/jquery"></script> <meta charset="utf-8"> <title>Ajax Demo</title> </head> <body> <button id="btn">发送请求</button> <script> $(document).ready(function(){ $('#btn').click(function(){ $.ajax({ url: '/api', type: 'GET', dataType: 'json', success: function(data){ console.log(data); }, error: function(xhr, status, error){ console.log(error); } }); }); }); </script> </body> </html>
The above code creates a button. When the button is clicked, a GET request is sent to Node using jQuery's $.ajax() function. /api route for .js service. After the request is successful, the console will output the JSON object returned from the server.
You can select the corresponding DOM element through the selector and bind the event processing function to realize data interaction between the page and the backend. This is one of the most important ways to use Ajax.
Finally, the request needs to be processed in the Node.js service. Add the following code to app.js:
app.use(express.static('public')); app.get('/', (req, res) => { res.sendFile(__dirname+'/public/index.html'); });
The above code adds a static file service to the Node.js service and sets the homepage to the index.html file in the public folder.
Now you can open http://localhost:3000 in the browser, click the "Send Request" button, the console will output the JSON object returned from the server, and the page will also display the "Hello World!" message .
Summary
This article introduces how to use Ajax to send Node.js requests, and provides a brief introduction to how the front end and back end interact. In this way, the front end can interact with the back end relatively easily, achieving richer functionality and a better user experience. Of course, Node.js' ability to handle HTTP requests is far more than this, and more complex and complete functions require in-depth study and practice.
The above is the detailed content of ajax sends nodejs request. For more information, please follow other related articles on the PHP Chinese website!