我是编程和新项目的新手,我正在使用Parse.com Cloud Code和Express构建一个Web应用程序。该结构是这样的:
基于URL参数显示动态网页内容(Parse Cloud,Express,ejs)
- app.js(快递相关代码)
- 的意见/ hello.ejs(模板)
- main.js(云功能)
我还有一个iOS应用可以在Facebook上发布链接,如下所示:myapp.parseapp.com/hello?objectid
。当Facebook用户点击该链接并重定向到网络应用程序时,网络应用程序将自动根据所提供的objectid
模板res.render
。然后使用objectid
从Parse类使用Parse.Query获取一些数据,这些数据将显示在页面上,但我的障碍不在于Parse.Query。
要检查是否objectid
成功地通过服务器所捕获,我尝试下面的代码来呈现模板,并插入objectid
到占位符text
变量,但put objectid here
当页面加载不改变所提供的objectid
。任何人都可以告诉我哪里出了问题,或者如果我应该用另一种方法做到这一点?
hello.ejs (template):
<!DOCTYPE html>
<html>
<head>
<title>My Web App</title>
<script src="https://ajax.googleapis.com/ajax/libs/jquery/1.11.3/jquery.min.js"></script>
<script type="text/javascript">
$.post('/hello', {oid: window.location.toString().split("?")[1]});
</script>
</head>
<body>
<p><%= text %></p>
</body>
</html>
app.js (express):
// Initialize Express in Cloud Code.
var express = require('express');
var app = express();
// Global app configuration section
app.set('views', 'cloud/views'); // Specify the folder to find templates
app.set('view engine', 'ejs'); // Set the template engine
app.use(express.bodyParser()); // Middleware for reading request body
// Render view
app.get('/hello', function(req, res) {
res.render('hello', { text: 'put objectid here' });
});
app.post('/hello', function(req, res) {
res.render('hello', { text: req.body.oid });
});
app.listen();
我在另一篇文章中找到了解决方案:http://stackoverflow.com/questions/20089582/how-to-get-url-parameter-in-express-node-js – Pork