Hi guys!
I have a problem using Ajax (side client) when I'm trying to request from
other domain, I know that this is caused by cross domain, but I can't find
any solution to fix it!
My app have to parse a HTML webpage to use some relevant information of it.
So I use Ajax to get it.
here is my code on the server, I'm using express.js:
/*
* Module dependencies
*/
var express = require('express')
, stylus = require('stylus')
, nib = require('nib')
, cors = require('express-cors')
//create the application
var app = express()
var allowCrossDomain = function(req,res,next){
res.header("Access-Control-Allow-Origin", "*");
res.header("Access-Control-Allow-Headers", "X-Requested-With");
next();
}
//compile style css with stylus module
function compile(str, path) {
return stylus(str)
.set('filename', path)
.use(nib());
}
app.set('views', __dirname + '/views')
app.set('view engine', 'jade')
app.use(express.logger('dev'))
app.use(allowCrossDomain);
app.use(stylus.middleware(
{ src: __dirname + '/public'
, compile: compile
}
))
app.use(express.static(__dirname + '/public'))
app.get('/', function (req, res) {
res.render('index',
{ title : 'Home' }
)
})
app.use(cors({
allowedOrigins: [
'google.es'
]
}))
//Server listening on port 3000
var server = app.listen(3000, function() {
console.log('Listening on port %d', server.address().port);
});
And here is my request with Ajax:
$.ajax({type: 'GET',
crossDomain: true,
url: 'https://www.google.es/',
dataType: "html",
timeout: 5000,
xhrFields: {
withCredentials: true
},
success: function(data) {
console.log(data);
},
error: function() { alert('Error!'); }
});
Also, I was trying to do this with other types of request, not only with
Ajax, but they didn't work neither.
Please any suggestion? What's wrong?
Thanks!!
--
Job board: http://jobs.nodejs.org/
New group rules:
https://gist.github.com/othiym23/9886289#file-moderation-policy-md
Old group rules:
https://github.com/joyent/node/wiki/Mailing-List-Posting-Guidelines
---
You received this message because you are subscribed to the Google Groups
"nodejs" group.
To unsubscribe from this group and stop receiving emails from it, send an email
to [email protected].
To post to this group, send email to [email protected].
To view this discussion on the web visit
https://groups.google.com/d/msgid/nodejs/df2dba65-35b7-46a5-b67e-aeb9f1e95629%40googlegroups.com.
For more options, visit https://groups.google.com/d/optout.