Ajax回调返回0


                    

在过去两天内,我试图解决自己的问题。 现在我需要帮助。 我的任务很简单。 jQuery ajax请求将用户名和密码发布到服务器,其中是python框架“ Flask”。 我做了一个非常简单的示例,但仍然无法正常工作。</ p>

ajax:</ p>

  $。ajax({
                     网址:“ http:// localhost:5000 / login”,
                     输入:“ POST”,
                     dataType:“ json”,
                     数据:值,//(带有用户名和密码的json)
                     complete:功能(响应){
                         console.log(response);
                     }

                 });
</ code> </ pre>

我试图重写成功和错误功能,但是结果是相同的。
一切返回:</ p>

 对象{readyState = 0,status = 0,statusText =“ error”,更多...}
</ code> </ pre>

烧瓶实施:</ p>

  @ app.route(“ / login”,Methods = [“ GET”,“ POST”])
def login_page():

     返回jsonify(redirect =“ index.html”)
</ code> </ pre>

标题:</ p>

响应:</ p>

 内容长度30
内容类型应用程序/ json
日期,2013年10月20日,星期日,格林尼治标准时间
服务器Werkzeug / 0.9.3 Python / 2.7.3
Set-Cookie会话= eyJfaWQiOnsiIGIiOiJOakkwTUdVM01HVXhNR0prWmpka05qQXXPRPRGcwT1dWbFlUQmhPRGRpT0RrPSJ9fQ.BUU42w.FDnVp15DxEsMPP_TsOpq8OzzU5 HttpOnly; 路径= /
</ code> </ pre>

请求:</ p>

 接受application / json,text / javascript,* / *;  q = 0.01
接受编码gzip,放气
接受语言cs,en-us; q = 0.7,en; q = 0.3
缓存控制无缓存
连接保持活动
内容长度102
内容类型应用程序/ x-www-form-urlencoded; 字符集= UTF-8
主机127.0.0.1:5000
来源http:// localhost
语法无缓存
引用http://localhost/login.html
用户代理Mozilla / 5.0(X11; Ubuntu; Linux x86_64; rv:24.0)Gecko / 20100101 Firefox / 24.0
</ code> </ pre>

我试图返回重定向,并且控制台网络日志向我显示了此返回,但是我发现ajax不会重定向( https://stackoverflow.com/a/17256609/2107985 )。
每次来自ajax函数的console.log都显示“错误”,尽管200 OK。</ p>

通过手动键入链接: http:// localhost:5000 / login </ code>到浏览器,它将正常返回json。</ p>
     </ div>

展开原文

原文

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个回答


您的请求说的是 Origin http:// localhost </ code>,但是您请求的URL是 http:// localhost:5000 / login </ code>。</ p>

您的控制台应向您发出警告:</ p>


   Access Control-Allow-Origin不允许

Origin http:// localhost </ code>。</ p>
</ blockquote>

同一来源政策限制了您读取数据的能力 。 您需要为两个请求使用相同的来源,或者规避它 </ p>
     </ div>

展开原文

原文

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.

Csdn user default icon
上传中...
上传图片
插入图片
抄袭、复制答案,以达到刷声望分或其他目的的行为,在CSDN问答是严格禁止的,一经发现立刻封号。是时候展现真正的技术了!
立即提问
相关内容推荐