uWSGI AJAX,读取请求 [英] uWSGI AJAX, reading a request

查看:438
本文介绍了uWSGI AJAX,读取请求的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我试图从背后nginx的一个WSGI服务器的Ajax响应(如果该事项)。我觉得我有问题读取请求,因为它似乎变量 request_body_size 始终为0;我得到的无要求的时候,我希望看到测试字符串警告框返回值。

我找不到这个或这方面的工作的例子很多文档,所以如果有人知道如何解决这个问题我将不胜感激。

我有这条巨蟒code。在WSGI脚本:

 导入操作系统,SYS
从CGI进口parse_qs

高清应用(ENVIRON,start_response):

    尝试:
        request_body_size = INT(environ.get('CONTENT_LENGTH',0))
    除(ValueError错误):
        request_body_size = 0

    上下文= {}
    如果request_body_size = 0!
        request_body = ENVIRON ['wsgi.input'。读(request_body_size)
        输出= parse_qs(request_body)
    其他:
        输出=没有要求


    状态=200 OK
    response_headers = [('内容类型,text / html的'),(内容长度,STR(LEN(输出)))]
    start_response(状态,response_headers)
    返回[输出]
 

这是发出请求(jQuery的)的JS:

 函数test_request()
{
    VAR test_string =测试字符串;
    $阿贾克斯({
        键入:GET,
        网址:/ AJAX
        数据:test_string,
        成功:函数(结果){警报(结果)}}
    );
}
 

解决方案

阿贾克斯()方法中的数据值转换为QUERY_STRING时,该方法是GET。使用POST应解决的问题(或者你可以阅读ENVIRON ['QUERY_STRING'])

Hi I'm trying to get an ajax response from a wsgi server behind nginx (if that matters). I think I'm having issues reading the request as it would seem the variable request_body_size is always 0; I get a return value of "No Request" in the alert box when I would like to see "test string".

I cant find many docs on this or examples of this working, so if anyone knows how to fix this I would be grateful.

I have this python code in the WSGI script:

import os, sys
from cgi import parse_qs

def application(environ, start_response):

    try:
        request_body_size = int(environ.get('CONTENT_LENGTH', 0))
    except (ValueError):
        request_body_size = 0

    context = {}
    if request_body_size != 0:
        request_body = environ['wsgi.input'].read(request_body_size)
        output = parse_qs(request_body)
    else:
        output = "No request" 


    status = '200 OK'
    response_headers = [('Content-type', 'text/html'),('Content-Length', str(len(output)))]
    start_response(status, response_headers)
    return [output]

and this is the js making the request (jquery):

function test_request()
{
    var test_string = "test string";
    $.ajax({
        type: 'GET',
        url:"/ajax", 
        data: test_string, 
        success:function(result){alert(result)}}
    );
}

解决方案

The ajax() method converts the 'data' value to a QUERY_STRING when the method is GET. Using POST should address the issue (or you can read environ['QUERY_STRING'])

这篇关于uWSGI AJAX,读取请求的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆