通过截击处理帖子(带有json字段)时,我在Node.Js上收到以下错误:
SyntaxError: Unexpected token % in JSON at position 0
at JSON.parse (<anonymous>)
在使用chrome浏览器时,一切正常(并且没有发生错误)。
Node.JS代码如下:
var express = require('express');
var app = express();
var bodyParser = require("body-parser");
const log = require('simple-node-logger').createSimpleLogger();
var server_port = 32082
var server_ip_address = ''
app.use(bodyParser.json());
app.use(bodyParser.urlencoded({extended : true}));
var server = app.listen(server_port, server_ip_address, function () {
var host = server.address().address
var port = server.address().port
log.info('App listening at ', host, ' ', port);
console.log( "Listening on " + server_ip_address + ", port " + server_port );
})
app.get('/', function (req, res) {
log.info("Request login page");
res.sendFile('login.html', {root: __dirname })
})
app.get('/css/style.css', function (req, res) {
log.info("Request style.css");
res.sendFile('/css/style.css', {root: __dirname })
})
app.get('/js/login.js', function (req, res) {
log.info("Request login.js");
res.sendFile('/js/login.js', {root: __dirname })
})
app.post('/login.html', function (req, res) {
//data = req.body.data;
log.info("Got MESSAGE ");
data = req.body.data;
var stringify = JSON.stringify(data)
log.info("Got: " , stringify);
...
some code...
...
})
java脚本文件(在测试相同场景时可以正常运行:login.html发布)看起来如下:
...
$.ajax({
type: 'POST',
url: "login.html",
data: { name: email, pass: password },
success: function(data) {
console.log("got welcome.html page");
document.write(response);
},
dataType: "json"
});
...
android代码(使用排球)并导致Node.Js出现错误:
private RequestQueue mRequestQueue;
private void sendWithVolly(String url)
{
Map<String, String> params = new HashMap<String, String>();
params.put("user", "root");
params.put("pass", "123456");
JsonObjectRequest jsonObjReq = new JsonObjectRequest(Request.Method.POST,
url, new JSONObject(params),
new Response.Listener<JSONObject>() {
@Override
public void onResponse(JSONObject response) {
}
}, new Response.ErrorListener() {
@Override
public void onErrorResponse(VolleyError error) {
}
}) {
@Override
protected Map<String, String> getParams() {
Map<String, String> params = new HashMap<String, String>();
params.put("Content-Type", "application/json");
return params;
}
};
mRequestQueue.add(jsonObjReq);
mRequestQueue.start();
}