【发布时间】:2015-09-20 01:11:01
【问题描述】:
我使用 angularjs 前端和 nodejs 作为后端,我很困惑为什么我的callobj 没有按预期工作。
我的相关nodejs代码是:
var client = require('twilio')(accountSid, authToken);
var unirest = require('unirest');
var express = require("express");
var http = require('http');
var app = express();
var o2x = require('object-to-xml');
var qs = require('querystring');
app.use(express.static('dist'));
app.get("/", function (req,res) {
res.render('index.html');
});
var callobj = {};
app.post("/call", function(req,res){
callobj.user = req.user;
callobj.phone = req.phone;
callobj.song = req.song;
callobj.mp3link = req.mp3link;
console.log(req.body);
console.log(callobj);
res.send(200);
});
app.get("/call", function(req,res) {
console.log(callobj);
res.set("Content-Type", "text/xml");
res.send(o2x({
'?xml version="1.0" encoding="utf-8"?' : null,
Response: {
Play: callobj.mp3link
}
}));
});
angularjs代码为:
var app = angular.module('angularexpressApp', []);
app.controller('appController', function($scope, $http){
$scope.user = 'Shawn';
$scope.phone = '5197227689'
$scope.song = 'Cant feel my face';
$scope.mp3link = 'https://api.twilio.com/cowbell.mp3';
$scope.mp3submit = function(){
$http.post('/call', {user: $scope.user, phone: $scope.phone, song: $scope.song, mp3link: $scope.mp3link})
.success(function(data){
//what to do here
})
.error(function(data){
console.log('Error: ' + data);
});
};
});
【问题讨论】:
-
有什么问题?
-
是的,问题不在于全局对象,弄错了对不起!
标签: javascript angularjs node.js