0

in last 2 days I tried to solve my problem. Now I need help. My task is very simple. Jquery ajax request posts a user name and a password to server, where is python framework "Flask". I did very simple example, but it still doesn't work.

ajax:

$.ajax({
                    url: "http://localhost:5000/login",
                    type: "POST",
                    dataType: "json",
                    data: values, //(json with username and password)
                    complete:function(response){
                        console.log(response);
                    }

                });

I tried to rewrite success and error function, but result is the same. Everything returns:

Object { readyState=0, status=0, statusText="error", more...}

Flask implementation:

@app.route("/login", methods=["GET", "POST"])
def login_page():

    return jsonify(redirect="index.html")

Headers:

response:

Content-Length  30
Content-Type    application/json
Date    Sun, 20 Oct 2013 09:50:19 GMT
Server  Werkzeug/0.9.3 Python/2.7.3
Set-Cookie  session=eyJfaWQiOnsiIGIiOiJOakkwTUdVM01HVXhNR0prWmpka05qQXlPRGcwT1dWbFlUQmhPRGRpT0RrPSJ9fQ.BUU42w.FDnVp15DxEsMPP_TsOpq8OzzU5I; HttpOnly; Path=/

request:

Accept  application/json, text/javascript, */*; q=0.01
Accept-Encoding gzip, deflate
Accept-Language cs,en-us;q=0.7,en;q=0.3
Cache-Control   no-cache
Connection  keep-alive
Content-Length  102
Content-Type    application/x-www-form-urlencoded; charset=UTF-8
Host    127.0.0.1:5000
Origin  http://localhost
Pragma  no-cache
Referer http://localhost/login.html
User-Agent  Mozilla/5.0 (X11; Ubuntu; Linux x86_64; rv:24.0) Gecko/20100101 Firefox/24.0

I tried to return a redirection, and console network log shows me this returning, but I have found the ajax doesn't redirect (https://stackoverflow.com/a/17256609/2107985). Every time console.log from ajax function shows me "error", although there is 200 OK.

By manually typing link: http://localhost:5000/login to the browser, it returns json normally.

Community
  • 1
  • 1
Meph-
  • 657
  • 1
  • 8
  • 20

1 Answers1

1

Your request says Origin http://localhost but the URL you are requesting is http://localhost:5000/login.

Your console should be giving you the warning:

Origin http://localhost is not allowed by Access-Control-Allow-Origin.

Your ability to read the data is limited by the same origin policy. You either need to use the same origin for both requests or circumvent it.

Community
  • 1
  • 1
Quentin
  • 914,110
  • 126
  • 1,211
  • 1,335
  • no warning. I added [these](http://stackoverflow.com/a/9570852/2107985) data to headers, but it didn't help. – Meph- Oct 20 '13 at 12:14
  • What, exactly, do the response headers say now? And what does the response body look like? – Quentin Oct 20 '13 at 12:34
  • response header has new rows: Access-Control-Allow-Head... Authorization, Access-Control-Allow-Meth...HEAD, GET, POST, OPTIONS, Access-Control-Allow-Orig...* and body contains {"redirect": "index.html"} – Meph- Oct 20 '13 at 13:45
  • Looking at the requests being made, do you see an OPTIONS request? Is it successful? – Quentin Oct 20 '13 at 13:48
  • Yes and its status was 200 OK, but from ajax I got error again :( – Meph- Oct 20 '13 at 14:47
  • Now, it is working :) Between some steps I add contentType:"json", after removing it, everything is fine ;) But I don't understand why. – Meph- Oct 20 '13 at 17:39