简体   繁体   English

NodeJS使用express返回JSONP NOT

[英]NodeJS return JSONP NOT using express

Im trying to call my nodejs server and show the results from an angularjs app. 我试图调用我的nodejs服务器并显示angularjs应用程序的结果。 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. 我觉得我的服务器有问题,但是我无法使它工作,它的nodejs没有express或其他库,所有其他答案都涉及使用express。 how can i make it work without using express? 如何在不使用快递的情况下使其工作?

the code in angularjs, where i call the server: angularjs中的代码,我调用服务器:

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. 如果我使用var api中的值,结果就可以了,调用成功,而不是我的。

This is the code in my nodejs server. 这是我的nodejs服务器中的代码。

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: 我很确定我必须将json结果包装在一个函数中,但我不知道如何,我甚至不知道函数名称,因为这是我在服务器中得到的所有内容:

path: /selectAllLocations params: {"callback":"angular.callbacks._0"} 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. 我理解我返回纯json,我可以在我的响应中看到它在chrome中,因此,有一些东西阻止它调用成功函数。 Can somebody point me in the right direction? 有人能指出我正确的方向吗?

函数名称被指定为callback参数。

 response.write(params.callback + '(' + JSON.stringify(rows) + ')');

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM