# Send a simple JavaScript

**URL:** <https://discourse.nodered.org/t/send-a-simple-javascript/36878>\
**Category:** General\
**Created:** [2 December 2020 14:40 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878 "2020-12-02T14:40:56Z")\
**Posts on this page:** 13\
**Page:** 1

<div class="post-metadata">

**Author:** ![Colla](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Colla](https://discourse.nodered.org/u/Colla)\
**Post date:** [2 December 2020 14:40 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/1 "2020-12-02T14:40:56Z")

</div>

Hi everyone!!! I'm new to this forum and to using node-red

I'm trying everything but i cannot get this simple javascript code to work

var xhr = new XMLHttpRequest();  
xhr.open("POST", "...url...", true);

xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");  
xhr.onreadystatechange = function() { // Call a function when the state changes.  
if (this.readyState === XMLHttpRequest.DONE && this.status === 200) {  
}  
}  
xhr.send("name=pippo&surname=topolino&street=blablabla");

Which NODE should be used ??  
Thanks for your help!!!

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2020 15:03 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/2 "2020-12-02T15:03:09Z")

</div>

Welcome to the forum @Colla

Is there a reason you don't want to use the http request node?

---

<div class="post-metadata">

**Author:** ![Colla](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Colla](https://discourse.nodered.org/u/Colla)\
**Post date:** [2 December 2020 15:09 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/3 "2020-12-02T15:09:44Z")

</div>

Thanks @Colin!

No, there isn't reason, but I already have the code in javascript ready

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 December 2020 15:10 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/4 "2020-12-02T15:10:33Z")

</div>

This should help...

> **[HTTP recipes : Node-RED](https://cookbook.nodered.org/http/)**

---

<div class="post-metadata">

**Author:** ![Colla](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Colla](https://discourse.nodered.org/u/Colla)\
**Post date:** [2 December 2020 15:19 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/5 "2020-12-02T15:19:11Z")

</div>

Ok but it's not clear to me how to integrate it with my javascript code

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2020 15:21 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/6 "2020-12-02T15:21:15Z")

</div>

Best to use the node, that is the node-red way.

---

<div class="post-metadata">

**Author:** ![Steve-Mcl](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/steve-mcl/32/4826_2.png) [@Steve-Mcl](https://discourse.nodered.org/u/Steve-Mcl)\
**Post date:** [2 December 2020 15:27 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/7 "2020-12-02T15:27:03Z")

</div>

Have you tried the cook book examples?

Also,

I recommend watching this playlist: [Node-RED Essentials](https://www.youtube.com/playlist?list=PLyNBB9VCLmo1hyO-4fIZ08gqFcXBkHy-6). The videos are done by the developers of node-red. They're nice & short and to the point. You will understand a whole lot more in about 1 hour. A small investment for a lot of gain.

---

<div class="post-metadata">

**Author:** ![Colla](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Colla](https://discourse.nodered.org/u/Colla)\
**Post date:** [2 December 2020 15:34 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/8 "2020-12-02T15:34:18Z")

</div>

Yes I looked at the examples, but they didn't help me

I also watched part of the NODE RED ESSENTIAL videos

I thought it was an easy thing to do. I'll do some more tests and document myself better

---

<div class="post-metadata">

**Author:** ![Colin](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/colin/32/17040_2.png) [@Colin](https://discourse.nodered.org/u/Colin)\
**Post date:** [2 December 2020 16:19 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/9 "2020-12-02T16:19:50Z")

</div>

> [@Colla](#):
>
> I looked at the examples, but they didn't help me

Do you mean you were not able to work out how to perform the http request using that node?

---

<div class="post-metadata">

**Author:** ![Colla](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Colla](https://discourse.nodered.org/u/Colla)\
**Post date:** [2 December 2020 16:22 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/10 "2020-12-02T16:22:46Z")

</div>

> [@Colin](#):
>
> Do you mean you were not able to work out how to perform the http request using that node?

exact...  
I think it's a "stupid" thing but I'm not succeeding!!!

---

<div class="post-metadata">

**Author:** ![knolleary](https://sea2.discourse-cdn.com/flex026/user_avatar/discourse.nodered.org/knolleary/32/3_2.png) [@knolleary](https://discourse.nodered.org/u/knolleary)\
**Post date:** [2 December 2020 16:33 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/11 "2020-12-02T16:33:47Z")

</div>

Hi @Colla

here is an example flow that should help you get started:

```auto
[{"id":"923bc595.5cc568","type":"http request","z":"d607ce33.4fa5a","name":"","method":"POST","ret":"txt","paytoqs":"ignore","url":"","tls":"","persist":false,"proxy":"","authType":"","x":570,"y":660,"wires":[["98fe22d2.28da2"]]},{"id":"b0a97530.5a0608","type":"inject","z":"d607ce33.4fa5a","name":"","props":[{"p":"payload"},{"p":"headers","v":"{\"content-type\":\"application/x-www-form-urlencoded\"}","vt":"json"}],"repeat":"","crontab":"","once":false,"onceDelay":0.1,"topic":"","payload":"{\"name\":\"pippo\",\"surname\":\"topolino\",\"street\":\"blablabla\"}","payloadType":"json","x":390,"y":660,"wires":[["923bc595.5cc568"]]},{"id":"98fe22d2.28da2","type":"debug","z":"d607ce33.4fa5a","name":"","active":true,"tosidebar":true,"console":false,"tostatus":false,"complete":"false","statusVal":"","statusType":"auto","x":770,"y":660,"wires":[]}]

```

Copy that JSON then import it into your Node-RED Editor using the "Import" menu option.

That will give you a flow that looks like this:

 ![image](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/3X/8/3/83680d05ca3ce9f81ad4bca174e0e209e8c5af14.png)

The Inject node sets two properties on the message:

`msg.payload` is set to a Object containing the data you want to send in the request:

```auto
{"name":"pippo","surname":"topolino","street":"blablabla"}

```

`msg.headers` is set to an Object containing the HTTP headers to use on the request:

```auto
{"content-type":"application/x-www-form-urlencoded"}

```

Next the HTTP Request node will do the actual request. I have configured it already to do a POST request - but you'll need to fill in the URL.

The result of the request will be passed to the final Debug node to display the result in the Debug Sidebar.

Once you've imported that to your workspace and set the URL property of the request node, hit the Deploy button, then trigger the Inject node by clicking its button.

I hope that's good enough to help you get started.

---

<div class="post-metadata">

**Author:** ![Colla](https://avatars.discourse-cdn.com/v4/letter/c/d78d45/32.png) [@Colla](https://discourse.nodered.org/u/Colla)\
**Post date:** [3 December 2020 08:35 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/12 "2020-12-03T08:35:26Z")

</div>

Hi @knolleary!

Thank you so much for the help and explanation!!  
Now it's much clearer how it works,

I was wrong to send the request correctly in the header: {"content-type":"application/x-www-form-urlencoded"}

Really thanks again for the support!!

---

<div class="post-metadata">

**Author:** ![system](https://us1.discourse-cdn.com/flex026/uploads/nodered/original/1X/d073cd938eafa2e558d7c2cd59003b3ef4963033.png) [@system](https://discourse.nodered.org/u/system)\
**Post date:** [1 February 2021 08:35 UTC](https://discourse.nodered.org/t/send-a-simple-javascript/36878/13 "2021-02-01T08:35:27Z")

</div>

This topic was automatically closed 60 days after the last reply. New replies are no longer allowed.
