weixin_33743880 2013-10-20 10:12 采纳率: 0%
浏览 58

Ajax回调返回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.

  • 写回答

1条回答 默认 最新

  • 七度&光 2013-10-20 10:59
    关注

    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.

    评论

报告相同问题?

悬赏问题

  • ¥15 如何在scanpy上做差异基因和通路富集?
  • ¥20 关于#硬件工程#的问题,请各位专家解答!
  • ¥15 关于#matlab#的问题:期望的系统闭环传递函数为G(s)=wn^2/s^2+2¢wn+wn^2阻尼系数¢=0.707,使系统具有较小的超调量
  • ¥15 FLUENT如何实现在堆积颗粒的上表面加载高斯热源
  • ¥30 截图中的mathematics程序转换成matlab
  • ¥15 动力学代码报错,维度不匹配
  • ¥15 Power query添加列问题
  • ¥50 Kubernetes&Fission&Eleasticsearch
  • ¥15 報錯:Person is not mapped,如何解決?
  • ¥15 c++头文件不能识别CDialog