jquery - Pure HTML form works, same query with Ajax fails -
this html forms send request, , google authentication works :
<form action="/auth/google" method="get"> <button type="submit"> login google </button> </form>
however, content of response (json) displayed in browser, html page, , replaces current page, reason. if prevent form submitting (i.e. e.preventdefault()
), nothing happens @ all. don't know how catch json.
so i'm trying make same call using ajax
<button type="submit" id="btngoogle"> login google </button> <script> $('#btngoogle').click( (e) => { $.ajax({ type: "get", url :"/auth/google", datatype : "json", crossdomain : true, // no effect headers : { // tried many headers, no effect } }) .success( (data) => { console.log("success!", data)) }) .error( (err) => { console.error(err.statustext); }) }); </script>
but cross-domain error
xmlhttprequest cannot load https://accounts.google.com/o/oauth2/v2/auth?response_type=code&redirect_ur…d=178168129307-5g50gah2he0gjjl2or73a1va8ha66s24.apps.googleusercontent.com. no 'access-control-allow-origin' header present on requested resource. origin 'http://localhost:8100' therefore not allowed access.
on node/express server :
app.get('/auth/google', passport.authenticate('google', { scope: ['https://www.googleapis.com/auth/plus.profile.emails.read'] }));
how come html form, reaches same server route same parameters, makes it,but ajax call doesn't?
edit
i have found workaround installing cors extension chrome, so... yeah, kind of works, why ajax call need this, whereas regular html form doesn't?
re: why ajax call need this, whereas regular html form doesn't":
your basic ajax
calls cannot process server redirects @ all, cannot handle redirect google's oauth system.
to avoid problem oauth providers had ensure login links not handled ajax , allowed browser handle redirects.
using iframe
handle external login , subsequent redirects option if want appear spa.
Comments
Post a Comment