POST

时间:2018-07-17 04:51:45

标签: javascript pyramid

我正在尝试做以下简单的事情??

  • 在测试页(“ / test ”)中,使用POST将一些参数发送到服务器
  • 当视图检测到某些参数时,请重定向到首页(“ / ”)

我可以使它起作用,但是只有当我使用表单发布值时,如果我运行调用 xmlhttprequest 的javascript函数,它就无法工作。我使用cookiecutter-starter,并仅添加了几行。

__ init __。py

from pyramid.config import Configurator

def main(global_config, **settings):
    """ This function returns a Pyramid WSGI application.
    """
    config = Configurator(settings=settings)
    config.include('pyramid_chameleon')
    config.add_static_view('static', 'static', cache_max_age=0)
    config.add_route('home', '/')
    config.add_route('test', '/test')
    config.scan()
    return config.make_wsgi_app()

views.py

from pyramid.view import view_config
from pyramid.httpexceptions import HTTPFound,HTTPSeeOther

@view_config(route_name='home', renderer='templates/mytemplate.pt')
def my_view(request):
    print("in my view")
    return {'project': 'project'}

@view_config(route_name='test', renderer='templates/jg.pt')
def y_view(request):
    prm_0 = request.POST.get("prm_0",None)
    prm_1 = request.POST.get("prm_1",None)
    if prm_0 and prm_1:
        print("parameters present")
        return HTTPFound(location=request.route_url("home"))
    else:
        print("no parameters found")
    return {}

templates / jg.pt

<?xml version="1.0" encoding="UTF-8"?>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:tal="http://xml.zope.org/namespaces/tal" xml:lang="es" lang="es">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
    <title>n</title>
    <script type="text/javascript">
    function postit(){
        console.log("pompom");
        var xhr = new XMLHttpRequest();
        xhr.open("POST", '/test', true);
        xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
        xhr.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
        xhr.send("prm_0=898&prm_1=603");
    }
    </script>
</head>
<body>
    using XMLHttpRequest..
    <br/>
    <button onclick="postit()">through xmlhttprequest</button>
    <br/>
    wrapped in a form...
    <form method="POST">
        <input type="hidden" name="prm_0" value="3455">
        <input type="hidden" name="prm_1" value="6778">
        <button type="submit">in-form</button>
    </form>
</body>

当我按下浏览器开发工具2中看到的xhr按钮时:

  • name=teststatus=302type=text,html
  • name=localhoststatus=200type=xhr

我可以在控制台中看到称为home的视图被调用(在“我的视图”中打印),但该视图无法渲染。

现在,当我按下表单按钮时,我在浏览器开发工具中看到了许多要求,这是最重要的:

  • name=teststatus=302type=text,html
  • name=localhoststatus=200type=document

这一次它确实呈现了主页。

我尝试调整xhr,以便标题看起来像另一个请求。

使用 xhr按钮发送的请求标头:

测试页

POST /test HTTP/1.1 
Host: localhost:6543 
Connection: keep-alive 
Content-Length: 19 
Pragma: no-cache 
Cache-Control: no-cache 
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8 
Origin: http://localhost:6543 
Upgrade-Insecure-Requests: 1 
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54 
Content-Type: application/x-www-form-urlencoded 
Referer: http://localhost:6543/test 
Accept-Encoding: gzip, deflate, br 
Accept-Language: es-419,es;q=0.9 
Cookie: pdtb_active=performance

主页

 GET / HTTP/1.1
 Host: localhost:6543
 Connection: keep-alive
 Pragma: no-cache
 Cache-Control: no-cache
 Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
 Upgrade-Insecure-Requests: 1
 User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54
 Referer: http://localhost:6543/test
 Accept-Encoding: gzip, deflate, br
 Accept-Language: es-419,es;q=0.9
 Cookie: pdtb_active=performance

现在使用表单按钮

测试页

POST /test HTTP/1.1
Host: localhost:6543
Connection: keep-alive
Content-Length: 21
Pragma: no-cache
Cache-Control: no-cache
Origin: http://localhost:6543
Upgrade-Insecure-Requests: 1
Content-Type: application/x-www-form-urlencoded
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://localhost:6543/test
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.9
Cookie: pdtb_active=performance

主页

GET / HTTP/1.1
Host: localhost:6543
Connection: keep-alive
Pragma: no-cache
Cache-Control: no-cache
Upgrade-Insecure-Requests: 1
User-Agent: Mozilla/5.0 (X11; Linux x86_64) AppleWebKit/537.36 (KHTML, like Gecko) Chrome/67.0.3396.87 Safari/537.36 OPR/54.0.2952.54
Accept: text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8
Referer: http://localhost:6543/test
Accept-Encoding: gzip, deflate, br
Accept-Language: es-419,es;q=0.9
Cookie: pdtb_active=performance

它们看起来一样!

为什么一个解释为xhr而另一个解释为document?

我在想,如果我将xhmbutton生成的请求视为可以呈现的文档,对吗?是这个吗?我想我在某处读到该页面由于引发的异常(httpfound)而无法呈现,但如果是这样,它将如何或何时起作用?

Calling another view in Pyramid中,有人建议使用“ render_to_response”或“ subrequests”,我尝试不成功,并查看了示例,这些示例均未对视图使用声明式样式,这使我认为这些生成器无法很好地处理选项生成。

完成此操作的正确方法是什么?我应该坚持表格吗?为什么表单有效,而另一种无效?

重复吗?有一点但重要的区别 Calling another view in Pyramid 在我的情况下,我使用声明式样式,并且视图的返回值通过装饰器中定义的渲染器。我认为这使其他线程的答案不适合该线程。

1 个答案:

答案 0 :(得分:1)

我在pylons-discuss邮件组中得到了答案。如果我正确理解由javascript进行的POST与使用表单进行的POST不相同。为此,javascript必须进行重定向。

完成后将js脚本更改为重定向,效果很好:

<script type="text/javascript">
function postit(){
    console.log("pompom");
    var xhr = new XMLHttpRequest();
    xhr.open("POST", '/test', true);
    xhr.setRequestHeader("Content-Type", "application/x-www-form-urlencoded");
    xhr.setRequestHeader("Accept", "text/html,application/xhtml+xml,application/xml;q=0.9,image/webp,image/apng,*/*;q=0.8");
    xhr.setRequestHeader("Upgrade-Insecure-Requests", 1);
    xhr.send("prm_0=898&prm_1=603");
    xhr.onreadystatechange = function() {
        if (this.readyState == 4 && this.status == 200) {
            window.location.replace("/");
        }
    };
}
</script>