0

I am using ajax call to send google sign in information to my backend. But the POST ajax request status is showing as 302 and then it gets converted to GET. I found many solutions for this but nothing worked for me.

function onSignIn(googleUser) {

  var profileObject = {}
  var profile = googleUser.getBasicProfile();
  var csrfmiddlewaretoken = 

  profileObject['name'] = profile.getName();
  profileObject['email'] = profile.getEmail();
  profileObject['csrftoken'] = getCookie('csrftoken');

  $.ajax({
    method: "post",
    url: '/login/',
    data: JSON.stringify(profileObject),
    contentType: "application/json;charset=utf-8",
    success: function (response) {

    }
  });
};

I am using python django framework.

Phil
  • 157,677
  • 23
  • 242
  • 245
Joshi R
  • 164
  • 1
  • 12
  • You have a typo: `type: "post"` should be `method: "post"`. – Blue Oct 21 '18 at 23:22
  • Thanks for pointing. Maybe I mistyped it while posting. 'method' is also giving the same result. – Joshi R Oct 21 '18 at 23:34
  • @FrankerZ is right and it should be `method: "POST"` in upper-case – oliverwebr Oct 21 '18 at 23:34
  • @FrankerZ `type` was the original jQuery `$.ajax()` property and it still exists as an alias for `method` so that's definitely not the problem – Phil Oct 21 '18 at 23:40
  • This isn't a problem. What you're seeing is a typical [post / redirect / get](https://stackoverflow.com/questions/10827242/understanding-post-redirect-get) flow. This is a very common pattern in handling HTTP POST requests – Phil Oct 21 '18 at 23:43
  • @qudrat POST is also not making any difference. – Joshi R Oct 21 '18 at 23:45
  • It is working fine when I am using the complete URL path i.e. "http://localhost:8000/en/accounts/login/". No Idea why it is behaving like this. – Joshi R Oct 21 '18 at 23:50
  • @JoshiR so looking at that, assuming your script is also running on `http://localhost:8000`, you want `url: '/en/accounts/login'`. When you prefix a URL path with a forward-slash, you're requesting a resource from the document root, so in your initial case, `/login/` resolves to `http://localhost:8000/login/`. See the difference? – Phil Oct 21 '18 at 23:53
  • Out of curiosity, what is the URL of the page running your JavaScript? – Phil Oct 21 '18 at 23:55
  • @Phil you are correct. it is getting resolved to http://localhost:8000/login/ – Joshi R Oct 22 '18 at 00:00
  • It is running on http://localhost:8000/en/register/ – Joshi R Oct 22 '18 at 00:01

0 Answers0