Note that there are some explanatory texts on larger screens.

plurals
  1. PONode JS Get Ajax request works but Post ajax request not works.Even Post data defined url is not responding
    primarykey
    data
    text
    <p>I am having a problem with writing express.js server, I tried to "post" and "get" some data I successfully "get" data via ajax, But when i "post" data rather than running .success(function (){...}), it runs .error(function(){}). I have been trying to figure out, matching code, but couldnt get the right pick, Please help ! Here is my Code of app.js on express.js</p> <pre><code>var express = require('express'); var routes = require('./routes'); var user = require('./routes/user'); var http = require('http'); var path = require('path'); var mydata = require('./routes/mydata'); var app = express(); {...}// some more default code app.get('/', routes.index); app.get('/users', user.list); app.get('/show',mydata.showData); app.get('/data',mydata.getData); app.get('/moredata',mydata.getMoreData); app.get('/moredata1', mydata.getMoreData1); http.createServer(app).listen(app.get('port'), function(){ console.log('Express server listening on port ' + app.get('port')); }); </code></pre> <p>Here is my code of mydata.js</p> <pre><code>exports.showData = function (req,res){ res.render('mydata') } exports.getData = function (req,res){ console.log('Hello from getData'); res.send('Hello from Server'); } exports.getMoreData = function (req,res){ console.log("Hello from getMoreData"); res.send({name:"Muhammad",age:23}); } exports.getMoreData1 = function (req,res){ console.log("Hello from getMoreData1"); console.log(req.body.name); console.log(req.body.age); res.send({name:"Faizan",age:55}); } </code></pre> <p>On client i have written this code filename : "mydataclient.js"</p> <pre><code>$(function(){ //alert("Its working"); $("#btn").on("click", function (){ //alert("Hello World"); $.ajax({ url:"/data", type:"get" }) .success(function(response){ console.log('success'); alert(response); $("#data").html(response); }) .error(function() { console.log("error from #btn"); }) }) $("#btnMore").on("click", function() { //alert("Hello World"); $.ajax({ url:"/moredata", type:"get" }) .success(function(response){ console.log('success'); alert(response); $("#data").html(response.name); }) .error(function() { console.log("error"); }) }) $("#btnPost").on("click", function() { var abc = $('#myname').val(); //alert(abc); $.ajax({ url:"/moredata1", type:"post", data: {name: abc, age:11} }) .success(function(response){ $("#data").html(response.name); }) .error(function(){ console.log("Error from btnPost"); }) }) }); </code></pre> <p>Everything on this page is working fine, Except when i try to post data to server it shows me this error on server console.</p> <blockquote> <p>POST /moredata1 404 5ms</p> </blockquote> <p>here is my page .ejs file code:</p> <pre><code>&lt;!DOCTYPE html&gt; &lt;html&gt; &lt;head&gt; &lt;title&gt;My Data Page&lt;/title&gt; &lt;link rel='stylesheet' href='/stylesheets/style.css' /&gt; &lt;script src ="/javascripts/jquery-2.0.3.min.js" type="text/javascript"&gt;&lt;/script&gt; &lt;script src ="/javascripts/mydataclient.js" type="text/javascript"&gt;&lt;/script&gt; &lt;/head&gt; &lt;body&gt; &lt;h1&gt;MyData&lt;/h1&gt; &lt;input type="text" id="myname"&gt; &lt;p&gt;Welcom &lt;/p&gt; &lt;div id="data"&gt;Hello&lt;/div&gt; &lt;button id="btn"&gt;Get Data&lt;/button&gt; &lt;button id="btnMore"&gt;Get More Data&lt;/button&gt; &lt;button id="btnPost"&gt;Post Data&lt;/button&gt; &lt;/body&gt; &lt;/html&gt; </code></pre> <p>You can also tell me alternative ways of posting data to server.</p> <p>Code is placed at <a href="https://www.dropbox.com/sh/0jzgt27897zxl1v/7VAraRWnMl" rel="nofollow">https://www.dropbox.com/sh/0jzgt27897zxl1v/7VAraRWnMl</a> Please Help !</p> <p>Thanks</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.
 

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