3

I had simple ajax request(request from external server)

             $.ajax({
                type: "POST",
                url: "http://localhost:8001/startApps",
                data: 'sss',
                success: function (data) {
                    console.log('AllOk');
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) {
                    console.log('err: '+XMLHttpRequest.status);
                }
            });

how i could get this data : 'sss' on node js server

app.post('/startApps', function (req,res,next) {
    console.log(req.body);//I trying req.data, req.params req.query etc ..
    res.send('Ok');
})
4
  • 1
    What does it return in the console? Commented Aug 25, 2017 at 14:00
  • @YanetPedraza req.data, req.body return undefined. req.query and req.params return {} Commented Aug 25, 2017 at 14:02
  • Did you check if you put your body-parser code above all the routes Commented Aug 25, 2017 at 14:06
  • @YanetPedraza bodyParser required var bodyParser = require('body-parser'); Commented Aug 25, 2017 at 14:12

3 Answers 3

1

Check body-parser in express:

var express = require('express'),
    app     = express(),
    port    = parseInt(process.env.PORT, 10) || 8080;

app.configure(function(){
  app.use(express.bodyParser());
  app.use(app.router);
});

app.listen(port);
Sign up to request clarification or add additional context in comments.

1 Comment

I forgot that that wasnt express project and i should to write app.use(bodyParser());
1

By default $.ajax defaults to application/x-www-form-urlencoded as the content type to send to server. In your case i hope you are sending plain data. Hence you should use contentType as text/plain to send data and fetch it on server as plain text.

 $.ajax({
            type: "POST",
            url: "https://localhost:8001/startApps",
            data: 'sss',
            contentType:'text/plain',
            success: function (data) {
                console.log('AllOk');
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log('err: '+XMLHttpRequest.status);
            }
        });

1 Comment

i add this line but i still couldnt get data on server(
1

Try this to see if it works

$.ajax({
            type: "POST",
            url: "http://localhost:8001/startApps",
            data: 'sss',
            dataType : "text",
            success: function (data) {
                console.log(data);
            },
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                console.log('err: '+XMLHttpRequest.status);
            }
});

app.post('/startApps', function (req,res,next) {
    res.send('Ok');
})

5 Comments

its work. I cat get response data from server. But how i could get request data on server?
Are you using bodyParser?
bodyPArser required var bodyParser = require('body-parser');
@ItsMyLife - Your data value sss needs a key so like: data: {key: 'sss'}, then you can access it using req.body.key
Try to put these lines above all your routes in your app.js file to see if it works app.use(bodyParser.urlencoded()); app.use(bodyParser());

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.