Note that there are some explanatory texts on larger screens.

plurals
  1. POHow do I retrieve the object I passed in jQuery.ajax()'s `data` attribute on the server side (node.js)?
    primarykey
    data
    text
    <p>I made a chrome extension which calls jQuery's <code>ajax</code> method:</p> <p><strong>content-script.js</strong></p> <pre><code>[...] $.ajax({ "url": "http://localhost:5000/", "type": "PUT", "contentType": "application/json", "data": { "name": "random object" }, "dataType": "json" }); [...] </code></pre> <p>On the server side, I'm trying to fetch the information passed in the <code>data</code> attribute:</p> <p><strong>web.js</strong></p> <pre><code>[...] app.put('/', function(request, response) { console.log(request.data); }); [...] </code></pre> <p>But I'm getting <code>undefined</code> instead. How is the object passed in the <code>data</code> attribute supposed to be retrieved on the server side (<a href="http://nodejs.org/" rel="nofollow">Node.js</a> + <a href="http://expressjs.com/" rel="nofollow">express.js</a>)?</p> <p>I also tried <code>console.log(request)</code> and got a LOT of stuff in there, but nothing that looked like the info I passed along in the <code>data</code> attribute...</p> <p><strong>EDIT</strong></p> <p>My latest attempt (based on graydsl's answer) brings the following changes to the code above:</p> <ul> <li>Added <code>app.use(express.bodyParser());</code> just before <code>app.put...</code>.</li> <li>Changed <code>put</code> to <code>post</code> everywhere</li> <li>Changed <code>request.data</code> to <code>request.body.data</code></li> </ul> <p>Now, the code does what I want when responding to a form like this:</p> <pre><code>&lt;form method="post" action="/"&gt; &lt;input name="data" type="text" value="WOO HA" /&gt; &lt;input type="submit" name="submit" value="submit" /&gt; &lt;/form&gt; </code></pre> <p>However it still fails if I revert to using <code>post</code> instead of <code>put</code> (and the browser ends up on <code>http://localhost:5000/?data=WOO+HA&amp;submit=submit</code> for some reason)</p> <p>It also fails when <code>put</code>ting (or <code>post</code>ing) with ajax (see code above). When I try it with ajax, I get this on the server:</p> <pre><code>SyntaxError: Unexpected token ILLEGAL at Object.parse (native) at IncomingMessage.&lt;anonymous&gt; (/home/shawn/.node_libraries/.npm/connect/1.8.7/package/lib/middleware/bodyParser.js:135:16) at IncomingMessage.emit (events.js:61:17) at HTTPParser.onMessageComplete (http.js:133:23) at Socket.ondata (http.js:1019:22) at Socket._onReadable (net.js:683:27) at IOWatcher.onReadable [as callback] (net.js:177:10) </code></pre>
    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.
 

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