我使用HTTP Post发布到我的Heroku服务器。当我把变量直接放在这样的服务器中时(我使用Postman):
var user = "Mélanie",
name = "Sortie au musée",
tag = "servertest";
它的工作,但当我使用(点击离子按钮)时:
var user = req.body.user,
name = req.body.name,
tag = req.body.tag;
我有内部错误500 ...
我的控制器:
var SERVER_SIDE_URL = "https://myapp.herokuapp.com";
var pushUrl = SERVER_SIDE_URL + "/cancelpush";
$scope.canceled = function() {
$http.post(pushUrl, {
params: {
user: "Mélanie",
name: "Sortie à la salle",
tag: "servertest"
}
}).success(function(data) {
console.log("Success bro !" + data)
}).error(function(data) {
alert("ERROR" + data);
});
};
我的服务器:
app.post('/cancelpush', function(req, res) {
var user = req.body.user,
name = req.body.name,
tag = req.body.tag;
var sendNotification = function(data) {
var headers = {
"Content-Type": "application/json",
"Authorization": "Basic MyKey(Secret)"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
var https = require('https');
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log("Response:");
console.log(JSON.parse(data));
});
});
req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
});
req.write(JSON.stringify(data));
req.end();
};
var message = {
app_id: "myappidwithnumbers(Secret)",
contents: {
"en": user + " not come " + name + " ! why ... "
},
tags: [{
"key": tag,
"relation": "=>",
"value": tag
}]
};
sendNotification(message);
});
堆栈追踪:
Request URL:https://myapp.herokuapp.com/cancelpush
Request Method:POST
Status Code:500 Internal Server Error
Remote Address:54.197.245.254:443
Response Headers
Access-Control-Allow-Credentials:true
Access-Control-Allow-Headers:X-Requested-With,content-type
Access-Control-Allow-Methods:GET, POST, OPTIONS, PUT, PATCH, DELETE
Access-Control-Allow-Origin:*
Connection:keep-alive
Content-Length:21
Content-Type:text/html
Date:Wed, 16 Mar 2016 19:54:18 GMT
Server:Cowboy
Via:1.1 vegur
X-Powered-By:Express
Request Headers
Accept:application/json, text/plain, */*
Accept-Encoding:gzip, deflate
Accept-Language:fr-FR,fr;q=0.8,en-US;q=0.6,en;q=0.4
Connection:keep-alive
Content-Length:66
Content-Type:application/json;charset=UTF-8
Host:mysterious-castle-64979.herokuapp.com
Origin:http://127.0.0.1:8103
Referer:http://127.0.0.1:8103/
User-Agent:Mozilla/5.0 (iPhone; CPU iPhone OS 9_1 like Mac OS X) AppleWebKit/601.1.46 (KHTML, like Gecko) Version/9.0 Mobile/13B137 Safari/601.1
Request Payload
{user: "Mélanie", name: "Sortie à la salle", tag: "servertest"}
name: "Sortie à la salle"
tag: "servertest"
user: "Mélanie"
请帮助我......谢谢!
编辑(问题解决,由于幻觉):错误的发生是因为未定义用户:
2016-03-16T18:10:58.833465+00:00 app[web.1]: TypeError: Cannot read property 'user' of undefined
2016-03-16T18:10:58.833503+00:00 app[web.1]: at Object.handle (/app/server.js:132:22)
2016-03-16T18:10:58.833508+00:00 app[web.1]: at next_layer (/app/node_modules/express/lib/router/route.js:103:13)
2016-03-16T18:10:58.833509+00:00 app[web.1]: at Route.dispatch (/app/node_modules/express/lib/router/route.js:107:5)
2016-03-16T18:10:58.833510+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:195:24
2016-03-16T18:10:58.833511+00:00 app[web.1]: at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:251:12)
2016-03-16T18:10:58.833512+00:00 app[web.1]: at next (/app/node_modules/express/lib/router/index.js:189:19)
2016-03-16T18:10:58.833512+00:00 app[web.1]: at Layer.handle (/app/server.js:50:5)
2016-03-16T18:10:58.833513+00:00 app[web.1]: at trim_prefix (/app/node_modules/express/lib/router/index.js:226:17)
2016-03-16T18:10:58.833514+00:00 app[web.1]: at /app/node_modules/express/lib/router/index.js:198:9
2016-03-16T18:10:58.833514+00:00 app[web.1]: at Function.proto.process_params (/app/node_modules/express/lib/router/index.js:251:12)
使用此服务器JS,它可以工作:
// init instances
var qs = require('querystring');
var request = require('request');
var express = require('express');
var app = express();
var bodyParser = require('body-parser');
var https = require('https');
// var stripe = require("stripe")(STRIPE_API_SECRET_KEY);
var app = express();
var router = express.Router();
var port = process.env.PORT || 9311;
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({
extended: false
}));
// Cross Domain Origin Setup
var allowCrossDomain = function(req, res, next) {
res.header('Access-Control-Allow-Origin', '*');
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header('Access-Control-Allow-Headers', 'Content-Type, Authorization, Content-Length, X-Requested-With');
// intercept OPTIONS method
if ('OPTIONS' == req.method) {
res.send(200);
} else {
next();
}
};
app.use(allowCrossDomain);
app.use(function(req, res, next) {
res.setHeader('Access-Control-Allow-Origin', '*');
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE');
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type');
res.setHeader('Access-Control-Allow-Credentials', true);
next();
});
router.get('/', function(req, res) {
res.json({
message: 'hola ! welcome to our triple problem last api!'
});
});
var url = 'http://requestb.in/wf21lpwf'
request(url, function(error, response, body) {
if (!error) {
console.log(body);
}
});
app.post('/cancelpush', function(req, res) {
// console.log(req);
// console.log(res);
var user = req.body.user;
var name = req.body.name;
var tag = req.body.tag;
console.log(require('util').inspect(req.body.user));
// var user = "Mélanie",
// name = "Sortie au musée",
// tag = "servertest";
var sendNotification = function(data) {
var headers = {
"Content-Type": "application/json",
"Authorization": "Basic MmNkNjc0YtYTcwODZmN2Y4MTYz"
};
var options = {
host: "onesignal.com",
port: 443,
path: "/api/v1/notifications",
method: "POST",
headers: headers
};
var https = require('https');
var req = https.request(options, function(res) {
res.on('data', function(data) {
console.log("Response:");
console.log(JSON.parse(data));
});
});
req.on('error', function(e) {
console.log("ERROR:");
console.log(e);
});
req.write(JSON.stringify(data));
req.end();
};
var message = {
app_id: "2549a5d6-e4d3afd941d9",
contents: {
"en": user + " notification " + name + " ! "
},
tags: [{
"key": tag,
"relation": "=>",
"value": tag
}]
};
console.log(require('util').inspect(message));
sendNotification(message);
});
app.use(bodyParser.urlencoded({
extended: true
}));
app.use(bodyParser());
app.use(bodyParser.json());
app.use('/api', router); // register our route
app.listen(port);
console.log('Magic happens on port ' + port);
我希望它可以帮助某人,感谢幻觉!
答案 0 :(得分:1)
执行$http.post
时,您的请求数据不需要包含在params
对象中。在您的客户端代码中,尝试使用以下代码替换$scope.canceled
:
$scope.canceled = function() {
$http.post(pushUrl, {
user: "Mélanie",
name: "Sortie à la salle",
tag: "servertest"
}).success(function(data) {
console.log("Success bro !" + data)
}).error(function(data) {
alert("ERROR" + data);
});
}