Hi, I’ve been fighting the whole day for the security constraint that makes your browser blocks your JS code when trying to reach an external server. Thas was solved on the standard Cross Origin Resource Sharing. (CORS for short). In my node JS server code I had to adapt the following:
//enable Cross Origin Resource Sharing
.use(function(req, res, next) {
app.header("Access-Control-Allow-Origin", "*");
res.header('Access-Control-Allow-Methods', 'GET,PUT,POST,DELETE,OPTIONS');
res.header("Access-Control-Allow-Headers", "Origin, X-Requested-With, Content-Type, Accept");
resnext();
; })
and on my Jquery I did:
//This is how your data looks like:
var data = {"answers":
["testNo":"1","answerNo":"2","answerValue":"answer1"},
{"testNo":"1","answerNo":"2","answerValue":"answer1"}
{,
]"userId":"idUser"};
// This is the object for the configuration:
var config = {};
.method = "PUT";
config.url = "http://localhost:8080/api/users";
config.contentType = "application/json";
config.data = JSON.stringify(data);
config.datatype = "text";
config
.ajax(config)
$.done(function(msg){
console.log(msg != null,"Function called: "+msg);
; })
The full code of this is available at my github account