Ajax Django Login/Authentication without re-direct

前端 未结 2 812
暗喜
暗喜 2021-01-15 15:54

I am trying to have an authentication set-up similar to that of StackOverflow, where the normal browsing is never affected unless there are some privileged actions which req

相关标签:
2条回答
  • 2021-01-15 16:02

    So, it looks like your current problem is with this: alert (json.server_response);. You may want to look into changing your $.ajax dataType parameter to json.

    To quote the docs:

    The type of data that you're expecting back from the server. If none is specified, jQuery will try to infer it based on the MIME type of the response (an XML MIME type will yield XML, in 1.4 JSON will yield a JavaScript object, in 1.4 script will execute the script, and anything else will be returned as a string). The available types (and the result passed as the first argument to your success callback) are:...

    "html": Returns HTML as plain text; included script tags are evaluated when inserted in the DOM.

    "json": Evaluates the response as JSON and returns a JavaScript object. The JSON data is parsed in a strict manner; any malformed JSON is rejected and a parse error is thrown. As of jQuery 1.9, an empty response is also rejected; the server should return a response of null or {} instead. (See json.org for more information on proper JSON formatting.)

    0 讨论(0)
  • 2021-01-15 16:05

    The approach I use is to have a Tastypie api layer and require authentication for the APIs. If the API call fails because of authentication, the client can request the user to log-in via the ajax login method.

    You can log-in a user via ajax using this gist

    0 讨论(0)
提交回复
热议问题