2

I am working on project containing app and landing pages. We are using Nodejs with Axios and VueJs for app part. But for landing pages, it is simple jQuery. I must do some API calls for landing pages, but I am not able to use NodeJs result in jQuery to serve data in my landing pages. I am new at NodeJs and these technologies. Here are my codes:

  • my Routes :

    const router = express.Router();
    ...
    router.get('/api/items', myApiController.getItems);
    
  • NodeJs controller

     module.exports.getItems = (req, res) => {
        const response = myApiController.getItems();
    
        if (response.status === 200) {
          res.send({
          status: 200,
          data: response.data
        })
      } else {
        res.send({
        status: 404,
        data: null
        })
      }
    }
    
  • my main script :

    $.get("/api/items", function(data, status){
        alert("Data: " + data);
        var mylist = $("#mylist");
        $.each(data, function(item) {
          mylist.append($("<option />").val(item.name).text(item.name));
        });
    });
    

Even if I am getting status:200 the nodejs is returning HTML of page 404. I do not find the cause, And honestly I do not understand the reason. It seems it is try to get a page does not exist, but I am requesting a json from function. I try to replace contoller method call by a trash json but nothing work. Here is what I try:

router.get('/api/items', function(req, res){
  console.log('cc');
  return res.json([{
    'toto': 'toto',
    'tata': 'tata',
  }]);
});

It seems routes definitions issue, but I do not know how to fix. Could this have something with express Router ? Could you please explain me and help me to fix this? Thanks

Cutis
  • 949
  • 1
  • 13
  • 32
  • don't you specify `application/json` as the content type anywhere in your code? – user3788685 Aug 05 '20 at 16:55
  • I think we need the code for `myApiController.getItems` since it is returning a response object that you are using in your `if/then` to decide what to pass to `res.send()` – mlibby Aug 05 '20 at 16:58
  • I dont think it is myApiController the problem. Can you check my last code part? I associate function to the router and the result is the same. – Cutis Aug 06 '20 at 08:51
  • @user3788685 no, I am trying to do by the simple possible way. But as I said, I am new in this app architecture. Do you have any idea to how I can change things to get the list ? Thanks – Cutis Aug 06 '20 at 08:55
  • @Cutis - check out [this](https://stackoverflow.com/questions/19696240/proper-way-to-return-json-using-node-or-express) question and answer it may help as we don't see enough of your code above – user3788685 Aug 07 '20 at 18:29
  • @user3788685 I have already checked it. that is not the same issue. thanks – Cutis Aug 11 '20 at 08:26

1 Answers1

1

When you respond with a string, the content type will be HTML. Try this, which removes the res.json call:

router.get('/api/items', function(req, res){
  console.log('cc');
  return [{
    'toto': 'toto',
    'tata': 'tata',
  }];
});
Steve H.
  • 6,912
  • 2
  • 30
  • 49
  • I do not know the difference with the last part of my code I pubish. But I try it and I am getting the same issue. What could be the issue? – Cutis Aug 06 '20 at 08:56