7
I am implementing an application where I need to perform a redirect
from one server to another and am encountering problems. While performing a redirect
, receive the following error message:
Xmlhttprequest cannot load https://servidordeaplicacao.com/endereco. The 'Access-Control-Allow-Origin' header has a value 'http://www.servidorhospedagemcliente.com' that is not Equal to the supplied origin. Origin 'null' is therefore not allowed access.
The flow of my application is as follows:
Follow the detail of the steps:
1º: Client requests a page from browser to server http://www.servidorhospedagemcliente.com/Teste.html
Request Header
GET /Test.html HTTP/1.1
Host: www.servoraccommodationcliente.com
Connection: Keep-Alive
Pragma: no-cache
Cache-Control: no-cache
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,/;q=0.8
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36 Accept-Encoding: gzip, deflate, sdch
Accept-Language: en,en;q=0.8,en-US;q=0.6,en;q=0.4
Response Header
HTTP/1.1 200 OK
Server: Apache/2.2.29 (Unix) mod_ssl/2.2.29 Openssl/1.0.1e-Fips mod_bwlimited/1.4
Connection: Keep-Alive Content-Type: text/html
2º O servidorhospedagemcliente
executes a ajax
requesting an address on servidornodejs
Ajax request made by Test.html
$("#testeAction").click(function() {
$.ajax({
success: function() {
alert('Funcionou Teste Action!');
},
error: function() {
alert('Agora deu ruim.');
},
type: 'GET',
url: 'https://servidornodejs.com/Teste'
});
});
Request Header
GET /HTTP Test/1.1
Host: servidornodejs.com
Connection: Keep-Alive
Accept: /
Origin: http://www.servidorhospedagemcliente.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Referer: http://www.servidorhospedagemcliente.com/Teste.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en,en;q=0.8,en-US;q=0.6,en;q=0.4
Response Header
HTTP/1.1 301 Moved Permanently
Server: Cowboy
Connection: Keep-Alive
X-Powered-By: Express
Access-Control-Allow-Origin: http://www.servidorhospedagemcliente.com
Vary: Origin, Accept
Location: https://servidoraplicacao.com/endereco
Content-Type: text/Plain; charset=utf-8
Content-Length
Date: Wed, 06 Jan 2016 18:36:50 GMT
Route: 1.1 vegur
3º O servidornodejs.com
upon receiving the request performs a redirect
to the servidordeaplicacao
Node JS code
'use strict';
var express = require('express');
var app = express();
var cors = require('cors');
var http = require('http');
app.set('port', (process.env.PORT || 5008));
var corsOptions = {
origin: 'http://www.servidorhospedagemcliente.com',
methods: 'GET,POST,PUT,DELETE,OPTIONS'
};
app.get('/Teste', cors(corsOptions), function(req, res, next) {
res.redirect(301,'https://servidordeaplicacao.com/endereco');
});
app.listen(app.get('port'), function() {
console.log('Node app is running on port', app.get('port'));
});
Request Header
GET /HTTP Test/1.1
Host: servidornodejs.com
Connection: Keep-Alive
Accept: /
Origin: http://www.servidorhospedagemcliente.com
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Referer: http://www.servidorhospedagemcliente.com/Teste.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en,en;q=0.8,en-US;q=0.6,en;q=0.4
Response Header
HTTP/1.1 301 Moved Permanently
Server: Server
Connection: Keep-Alive
X-Powered-By: Express
Access-Control-Allow-Origin: http://www.servidorhospedagemcliente.com
Vary: Origin, Accept
Location: https://servidordeaplicacao.com/endereco
Content-Type: text/Plain; charset=utf-8
4º O servidordeaplicacao
received the request and will make a redirect
to the servidornodejs
be able to redirect to servidorhospedagemcliente
to complete the steps 5 and 6. But there is the problem, the error message appears at that time, with the following header:
Node JS code
app.get('/Cliente', cors(corsOptions), function(req, res, next) {
res.redirect(301,'http://servidorhospedagemcliente.com/ok.html');
});
Java Application Code
public void enviar(HttpServletRequest request, HttpServletResponse response, String id) throws ServletException, IOException {
try {
{
if (request.getMethod().equals("OPTIONS") || request.getMethod().equals("GET") || request.getMethod().equals("POST")) {
response.addHeader("Access-Control-Allow-Origin", "http://www.servidorhospedagemcliente.com");
response.setHeader("Access-Control-Allow-Methods", "GET,POST,PUT,DELETE,OPTIONS");
response.setHeader("Access-Control-Allow-Headers", "Content-Type");
}
String url = "https://servidornodejs.com/Cliente";
response.setStatus(HttpServletResponse.SC_MOVED_PERMANENTLY);
response.addHeader("Location", url);
}
} catch (Exception e) {
e.printStackTrace();
throw new ServletException(e);
}
}
Request Header
GET /HTTP address/1.1
Host: room service.com
Connection: Keep-Alive
Accept: /
Origin: null
User-Agent: Mozilla/5.0 (Windows NT 10.0; WOW64) Applewebkit/537.36 (KHTML, like Gecko) Chrome/47.0.2526.106 Safari/537.36
Referer: http://www.servidorhospedagemcliente.com/Teste.html
Accept-Encoding: gzip, deflate, sdch
Accept-Language: en,en;q=0.8,en-US;q=0.6,en;q=0.4
Response Header
HTTP/1.1 301 Moved Permanently
Connection: close
Date: Wed, 06 Jan 2016 18:36:51 GMT
Content-Type: text/html;charset=utf-8
Access-Control-Allow-Origin: http://www.servidorhospedagemcliente.com
Access-Control-Allow-Methods: GET,POST,PUT,DELETE,OPTIONS
Access-Control-Allow-Headers: Content-Type
Now comes my question, the moment a redirect
is executed, it is possible to change Origin
HTTP header and can send? In my header it is null
, and according to the browser error message, Origin 'null' is therefore not allowed access.. Does anyone have any idea how to solve this problem?
Interesting question
+1
. Just a curiosity, why redirect? cannot make the Node server make an HTTP request to the Java server and fetch what you need to answer the client via Node?– Sergio
@Sergio I am using redirect because I could not make a request from Node to the Java server. Would you have an example of how to generate a request from Node JS that is easy to understand? I’m starting now with Node and I still don’t have much fluency in this technology, and from the documentation, I still can’t figure out how to do this.
– Duds
Take a look here: https://github.com/visionmedia/superagent is a mto library used to make ajax server side.
– Sergio
I’ll take a look at this bibilioteca. Thank you very much @Sergio!
– Duds