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.

    评论

报告相同问题?

悬赏问题

  • ¥35 平滑拟合曲线该如何生成
  • ¥100 c语言,请帮蒟蒻写一个题的范例作参考
  • ¥15 名为“Product”的列已属于此 DataTable
  • ¥15 安卓adb backup备份应用数据失败
  • ¥15 eclipse运行项目时遇到的问题
  • ¥15 关于#c##的问题:最近需要用CAT工具Trados进行一些开发
  • ¥15 南大pa1 小游戏没有界面,并且报了如下错误,尝试过换显卡驱动,但是好像不行
  • ¥15 自己瞎改改,结果现在又运行不了了
  • ¥15 链式存储应该如何解决
  • ¥15 没有证书,nginx怎么反向代理到只能接受https的公网网站