Kselax.ru

Hacker Kselax – the best hacker in the world

Menu
  • Blog
  • Contacts
  • wp plugin generator
  • English
    • Русский
Menu
ajax in node.js

How to make an ajax request in node.js

Posted on 5 July, 201820 July, 2018 by admin

To make an ajax request in node.js not obviously how it works, there not exists $_FILE array like in PHP and you have to install a few modules for extracting the passed data.

 

Types of forms

There could be two type forms, a form that passed files to a server (multipart/form-data) and a form that passes only data.

For the forms with multipart data, we use one of the module formidable or others.

For the forms without multipart we use simply body-parser 

 

Code for sending the ajax request to the server

The code is different for the two types of forms

Code for the forms without sending files

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
jQuery.ajax({
  url: '/login',
  type: 'POST',
  data: {
    name: name,
    pass: pass,
  },
  // processData: false, //allow send not only string
  // contentType: false,
  // async: false,// waiting till finished handling request
 
  success: function(msg){
    console.log("msg=",msg);
  }
});//end ajax

and the code for the forms with multipart

1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
jQuery.ajax({
  url: '/login',
  type: 'POST',
  data: {
    name: name,
    pass: pass,
  },
  processData: false, //allow send not only string
  contentType: false,
  // async: false,// waiting till finished handling request
 
  success: function(msg){
    console.log("msg=",msg);
  }
});//end ajax

 

the difference with multipart have to be this two rows

1
2
processData: false, //allow send not only string
contentType: false,

without it will not work

 

 

 

 

 

Leave a Reply Cancel reply

Your email address will not be published. Required fields are marked *

Categories

  • bash (1)
  • English (9)
  • JavaScript (4)
  • node.js (22)
  • photoshop (1)
  • php (3)
  • React (9)
  • sclerotic (6)
  • Ubuntu (10)
  • Uncategorized (13)
  • Wordpress (1)

Tags

Ajax apache2 automation bash chrome-extension command line editor ejs email English English-grammar framework functions git graphql handlebars hybrid app installation javascript js linux newbie node.js node.js javascript nodemailer npm objects Performance php phpmyadmin playonlinux promise rabbitmq React react-router redis reverse-proxy session shell socket.io sublime text 3 time zones ubuntu unity webpack

Recent Comments

  • damien on How to install npm and nodejs the latest versions on ubuntu
  • Cam on How to install npm and nodejs the latest versions on ubuntu
  • Pierre on socket.io with apache as a reverse proxy on the CentOS
  • admin on How to use react-router with a few languages
  • admin on How to install npm and nodejs the latest versions on ubuntu
©2021 Kselax.ru Theme by ThemeGiant