3

Im trying to call my nodejs server and show the results from an angularjs app. ive been following an example, but when i change the example code for my code, it always calls the error callback. i have a feeling its something wrong with my server, but i cant make it work, its nodejs without express or other libraries, and all other answers involve using express. how can i make it work without using express?

the code in angularjs, where i call the server:

app.controller('Main', function ($scope, $http) {
    $scope.init = function() {        
        //var api = 'http://api.trakt.tv/calendar/premieres.json/7c989229f2fa626832e7576eb59820e9/20131103/30/?callback=JSON_CALLBACK';        
        $http.jsonp('http://localhost:8888/selectAllLocations?callback=JSON_CALLBACK').success(function(data) {
            console.log(data);
        }).error(function(error) {
            console.log('My error: ' + error);
        });
    };

});

if i use the value in the var api, the result is ok and success is called, not with mine.

This is the code in my nodejs server.

var result = sequelize.query(query, null, options, parameters)
.success(function (rows) {        
    if (type == 'select') {
        response.writeHead(200, { "Content-Type": "application/json" });
        response.write(JSON.stringify(rows));
        response.end();
    } else if (type == 'modify') {
        response.writeHead(200, { "Content-Type": "text/html" });
        response.write("Query was successful");
        response.end();
    }
}
).error(function (e) {
    console.log("An error occured: ", e);
    response.writeHead(404, { "Content-Type": "text/html" });
    response.write("There was an error man, shits on fire yo ---> " + e);
    response.end();
}
);

Im pretty sure i have to wrap the json result in a function, but i dont know how, and i cant even know the function name, as this is all i get in my server:

path: /selectAllLocations params: {"callback":"angular.callbacks._0"}

I understand im returning the pure json, and i can see it in chrome in my response, so, there is something preventing it from calling the success function. Can somebody point me in the right direction?

1 Answer 1

4

The function name is specified as the callback parameter.

 response.write(params.callback + '(' + JSON.stringify(rows) + ')');
Sign up to request clarification or add additional context in comments.

6 Comments

You are right! that worked! the {"callback":"angular.callbacks._0"} i was getting is how angularjs renames the callback, and its another value in the parameters received, now it all makes sense. Do i have to wrap ALL my answers in that function, even if im not returning a json?
Also, im beginning to question if im doing right in not using a nodejs framework like express, as all questions here are using it.
@Toddy Since you're writing a web service, I'd recommend using restify, which is similar to express but less oriented to building web pages and more oriented towards services. At the very least, this will take care of getting the output format right (callbacks and all) for you.
Thanks Dan, i guess you are right. i read about it but decided to use node as is, to learn javascript, ill get to the rewrite, now that i understand some more things about javascript. id upvote you if i could!
This answer is basically correct, but I also like to set Content-Type: "application/javascript" when return JSONP, as it's no longer really JSON data being returned.
|

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.