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

6
  • don't you specify application/json as the content type anywhere in your code? Commented Aug 5, 2020 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() Commented Aug 5, 2020 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. Commented Aug 6, 2020 at 8: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 Commented Aug 6, 2020 at 8:55
  • @Cutis - check out this question and answer it may help as we don't see enough of your code above Commented Aug 7, 2020 at 18:29

1 Answer 1

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',
  }];
});
Sign up to request clarification or add additional context in comments.

1 Comment

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?

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.