2016-07-21 6 views
0

node.js를 사용하여 Google 인증을 사용하여 로그인하려고하는데 아래 오류가 표시됩니다.요청한 리소스에 Access-Control-Allow-Origin '헤더가 있습니다.

XMLHttpRequest를로드 할 수 https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur ... D = 410427634474-u3tpasmj4r80s6v20o54s85fikhotl79.apps.googleusercontent.com. 'Access-Control-Allow-Origin'헤더가 요청 된 리소스에 없습니다. 따라서 원본 'null'은 액세스가 허용되지 않습니다.

누군가이 문제를 해결하는 데 도움을 주시기 바랍니다. 지난 2 일 동안이 문제를 해결하려고했기 때문에 아직 해결하지 못했습니다.

+0

가능한 중복 of "["No 'Access-Control-Allow-Origin'헤더가 요청 된 리소스에 있음]] (http://stackoverflow.com/questions/20035101/no-access-control-allow-origin-header-is-present - 요청한 자원) –

답변

0

app.get 함수에 작성하십시오.

app.get("/png",function(req,res){ 
    res.set('Access-Control-Allow-Origin', '*'); 
    var sr = { data: "", message: "", error: "" }; 
    console.log(222); 
connection.query('SELECT * FROM png ', function(err, rows, fields) { 
connection.end(); 
    if (!err) 
    { 
     var userr = rows; 
     sr.data = userr; 
     res.json(sr); 

    } else{ 
    console.log('Error while performing Query.'); 
    } 
    }); 
}); 
+0

이 링크를 보시고 저에게 무엇이 잘못 됐는지 말씀해 주시겠습니까? [이것 역시이 문제에 관해 게시했습니다] (http://stackoverflow.com/questions/38472981/login-with-google-to-my-application-using- node-js-and-passport-authentication) – Kevin

3

시도가 NodeJS/특급 응용 프로그램에 다음 미들웨어를 추가로

res.set('Access-Control-Allow-Origin', '*'); 

당신은 (나는 당신의 편의를 위해 몇 가지 의견을 추가 한) 노드 기능을 사용할 수 있습니다 :

// Add headers 
app.use(function (req, res, next) { 

// Website you wish to allow to connect 
res.setHeader('Access-Control-Allow-Origin', '*'); 

// Request methods you wish to allow 
res.setHeader('Access-Control-Allow-Methods', 'GET, POST, OPTIONS, PUT, PATCH, DELETE'); 

// Request headers you wish to allow 
res.setHeader('Access-Control-Allow-Headers', 'X-Requested-With,content-type'); 

// Set to true if you need the website to include cookies in the requests sent 
// to the API (e.g. in case you use sessions) 
res.setHeader('Access-Control-Allow-Credentials', true); 

// Pass to next layer of middleware 
next(); 
}); 
+0

잘됐다. –

+0

이 작업을 끝내면 같은 오류가 발생합니다. 이 링크를 참조하십시오 [이것은 실제로 내가 뭘] (http://stackoverflow.com/questions/38472981/login-with-google-to-my-application-using-node-js-and-passport-authentication) – Kevin

+0

@ 케빈 : 다음과 같이 시도해주십시오 : router.get ('/ auth/google/callback', function (req, res, next) { \t res.set ('Access-Control-Allow-Origin', '*') passport.authenticate ("구글, 함수 (ERR, 사용자 정보) { 경우 (사용자 === 거짓) { \t \t \t \t \t \t \t \t .... } \t \t}); \t}); –

관련 문제