0

My get ajax isn't getting the response:

$.get(
    '/api/entity',
    function(item){
        console.log(item); //<-- doesn't show anything at all
    }
);

However, when I go to /api/entity on the browser or check the dev tools, I see the response I'm expecting from the request. Furthermore, when I log XMLHttpRequests, I get XHR finished loading: GET.

I changed the method of how I get data from mongodb. Instead of sending an array of all the data via callback, I'm now piping the stream directly to res.

Previous method:

db.entity.find({},function(err,data){
    res.end(data);
});

Current method:

db.entity.find({}).cursor({transform:JSON.stringify}).pipe(res);

I need to know how to get the response on ajax.

Eyzi
  • 516
  • 1
  • 6
  • 20
  • Is the browser get to the function? I mean if you put on this line a break point.. – Mosh Feu Apr 10 '18 at 17:07
  • Hi, Mosh. It doesn't seem to get to the function at all. That's mainly my question. I know there is a response, but the function isn't getting called. – Eyzi Apr 10 '18 at 20:27
  • You can try to implement it with `done`. Read this [answer](https://stackoverflow.com/a/8840315/863110) for more info. Also, have you tried to catch an error? Read [here](https://api.jquery.com/jquery.get/#jqxhr-object) how to do this. – Mosh Feu Apr 11 '18 at 04:30
  • Thanks, Mosh. I implemented `done`, `fail`, and `always`. `done` didn't run but `fail` and `always` did. Both have the status of 200 and statusText of 'parseerror'. They also both have all the data I needed in the responseText. – Eyzi Apr 11 '18 at 14:31
  • Maybe this [answer](https://stackoverflow.com/a/11507572/863110) could help you with it. – Mosh Feu Apr 11 '18 at 15:52

0 Answers0