Note that there are some explanatory texts on larger screens.

plurals
  1. PO
    primarykey
    data
    text
    <p>Your code is almost right but on your code sample you have </p> <pre><code>console.log(data[0].id) </code></pre> <p>the <strong>data</strong> object has <strong>no</strong> property <strong>id</strong> so if you only have </p> <pre><code>console.log(data[0]) </code></pre> <p>there you have a response like </p> <pre><code>{ a: '1' } </code></pre> <p>therefore you can access the property <strong>a</strong> by doing</p> <pre><code>console.log(data[0].a); </code></pre> <p><strong>UPDATED Updated with a full example</strong></p> <p>One more thing is that you are using <strong>eval</strong> and node comes with <strong>JSON.parse</strong> bundle with it so the snippet below is how i made it work </p> <p>File: app.js</p> <pre><code>var sys = require("sys"), http = require("http"), url = require("url"), path = require("path"), fs = require("fs"); http.createServer(function(request, res) { var parsed_url = url.parse(request.url); var uri = parsed_url.pathname; if(uri === "/test"){ res.writeHead(200, {'Content-Type': 'text/javascript'}); request.addListener('data', function (chunk) { // removed this - eval("(" + chunk + ")"); var data = JSON.parse(chunk); console.log(data[0].a); }) request.addListener('end', function() { console.log('end triggered'); res.write("Post data"); res.end(); }); } else if(uri === "/") { fs.readFile("./index.html",function(err, data){ if(err) throw err; res.writeHead(200, {'Content-Type': 'text/html'}); res.end(data); }); } }).listen(8080); </code></pre> <p>On the same directory create a file index.html with the following: </p> <pre><code>&lt;html&gt; &lt;head&gt; &lt;script type="text/javascript" charset="utf-8"&gt; var myhttp = new XMLHttpRequest(); var url = "http://localhost:8080/test"; var data = [{"a":"1"},{"b":"2"},{"c":"3"}]; var dataJson = JSON.stringify(data); myhttp.open('POST', url, true); myhttp.send(dataJson); myhttp.onreadystatechange = function() { if ((myhttp.readyState == 4) &amp;&amp; (myhttp.status == 200)){ alert(myhttp.responseText); } else if ((myhttp.readyState == 4) &amp;&amp; (myhttp.status != 200)) { console.log("Error in Connection"); } } &lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>That is a complete working example of what you want.</p> <p>With regards to the same origin policy issues you were having is mainly due to the fact that you cant POST data between 2 different domains via ajax unless you use some tricks with iframes but that is another story.</p> <p>Also i think is good for anyone to understand the backbone of a technology before moving into frameworks so fair play to you.</p> <p>good luck</p>
    singulars
    1. This table or related slice is empty.
    plurals
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. This table or related slice is empty.
    1. VO
      singulars
      1. This table or related slice is empty.
    2. VO
      singulars
      1. This table or related slice is empty.
    3. VO
      singulars
      1. This table or related slice is empty.
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload