我想从客户端向我的服务器发送一个jsonstring。我很确定我发送它没关系,但我似乎无法弄清楚如何在服务器端读取它。变量'sendJSONString'就是我要发送的内容。我见过很多例子,其中数据是在URL中发送然后提取的。但我真的不愿意这样做。
在客户端,我有这个代码:
module.exports = function sendAuthToServer(retVal, _userName, _AUTHKey){
var sendJSONString = JSON.stringify({userName:_userName, AUTHKey:_AUTHKey});
var gotData = [];
var xhr = new XMLHttpRequest();
xhr.open('GET', encodeURI('/AUTH'));
xhr.onload = function() {
if (xhr.status === 200) {
gotData = JSON.parse(xhr.responseText);
retVal(gotData);
}
else {
alert('Request failed. Returned status of ' + xhr.status);
}
};
xhr.send(sendJSONString);
}
在服务器端,我有这个。
module.exports = function giveQueryList(app){
app.get('/AUTH', function(req, res) {
res.json('ServerResponse');
console.log(req.ip, req);
});
}
app正在使用快递,并且设置如下
//CONFIG ROUTER/SERVER
app.set('view engine', 'ejs');
app.set('views', path.join(__dirname, 'views'));//MAKE CONTENT IN VIEWS FOLDER AVAILABLE TO CLIENT
app.set('port', process.env.PORT || 8888);
//USE MIDDLEWARE
app.use(express.static(path.join(__dirname, 'views')));
答案 0 :(得分:1)
您正在使用GET HTTP动词发送数据。尝试将其更改为POST或PUT(您需要相应地调整您的快速路线)。
如果尚未完成,则应使用 var root, records, f, n, doc;
doc = app.activeDocument;
root = doc.xmlElements[0];
records = root.evaluateXPathExpression ( "./record" );
n = records.length;
while ( n-- ) {
var ff = new Folder(Folder.desktop + "/" +app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlAttributes.item(0).value + "/data/" +
app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlElements.item(0).xmlElements.item(1).xmlAttributes.item(1).value + "/" +
app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlElements.item(0).xmlElements.item(2).xmlAttributes.item(1).value);
if (!ff.exists)
ff.create();
f = File ( ff +"/"+app.activeDocument.xmlElements.item(0).xmlElements.item(n).xmlElements.item(0).xmlElements.item(0).xmlElements.item(0).xmlAttributes.item(1).value);
records[n].exportFile ( ExportFormat.XML, f);
}
<?xml version="1.0" encoding="UTF-8"?>
<xsl:stylesheet version="1.0" xmlns:xsl="http://www.w3.org/1999/XSL/Transform">
<xsl:template match="/">
<xsl:for-each select="Root/record">
<record name="{@name}" type="{@type}">
<item name="{item/@name}">
<value>
<xsl:for-each select="item/value/item">
<item name="{@name}">
<value><xsl:value-of select="@value"/></value>
</item>
</xsl:for-each>
</value>
</item>
</record>
</xsl:for-each>
</xsl:template>
</xsl:stylesheet>
之类的中间件来解析客户端发送的信息。然后,您可以访问body-parser
对象上客户发布/发布的信息。