I have set up an express server to handle get requests to specific url endpoints. When responding to these requests, I am sending back data in JSON format to enable making Ajax calls and retrieving data from the page. To allow for cross-domain requests, I utilized jsonp. However, upon completion of the request and retrieval of the data within the callback function, an error is displayed:
Uncaught SyntaxError: Unexpected token :
.
Here are snippets of the server and client codes:
Server
var express=require('express');
var app = express();
var http = require('http').Server(app);
var port=Number(3000);
var server_ip_address = '127.0.0.1';
http.listen(port,server_ip_address, function(){
console.log('listening on '+port);
});
app.get('/test', function (req, res) {
console.log(req.query);
res.send({message:'hello'});
});
Client
$.ajax({
url: 'http://localhost:3000/test',
type: 'GET',
dataType:'jsonp',
crossDomain:true,
data: {
username: $('#username').val(),
password: $('#pwd').val()
},
success: function(result){
console.log($.parseJSON(result));
},
jsonp:'jsonp'
});
Within the server code, I attempted using
res.setHeader('Content-Type', 'application/javascript');
res.send({message:'recieved'});
as well as
res.setHeader('Content-Type', 'application/json');
res.send({message:'recieved'});
In the client side, I tried logging the result object directly without success. Any assistance would be greatly appreciated. Additionally, suggestions for alternative methods of fetching data are welcomed. Thank you in advance.