Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/python/360.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
如何通过post从javascript发送json并在python wsgi中读取_Javascript_Python - Fatal编程技术网

如何通过post从javascript发送json并在python wsgi中读取

如何通过post从javascript发送json并在python wsgi中读取,javascript,python,Javascript,Python,我有一个用例,需要将一个请求从我的javascript发布到python.wsgi,我想将请求数据作为json发送 在我的json中,代码如下所示: var aclient = HttpClient(); var pay_load = {what:"USERS", uname:username, password:pwd, index:"0"}; setTimeout(aClient.get("POST",alluserUrl, getAllUsersCallBack, JSON.string

我有一个用例,需要将一个请求从我的javascript发布到python.wsgi,我想将请求数据作为json发送

在我的json中,代码如下所示:

var aclient = HttpClient(); 
var pay_load = {what:"USERS", uname:username, password:pwd, index:"0"};
setTimeout(aClient.get("POST",alluserUrl, getAllUsersCallBack, JSON.stringify(pay_load)), 50);
其中aclient.get是:

var HttpClient = function()
{
this.get = function(aMethod, aUrl, aCallback, payload) {
var anHttpRequest = new XMLHttpRequest();

    anHttpRequest.onreadystatechange = function() {
        if (anHttpRequest.readyState == 4 && anHttpRequest.status == 200)
        {
            //console.log("triggering callback");
            aCallback(anHttpRequest.responseText);
            //return anHttpRequest.responseText;
        }
        else
        {
            //console.log("not 200");
            //console.log(anHttpRequest.status);
            //aCallback(sample);
            //console.log(sample);
        }

    };

    if ("GET" == aMethod)
    {
        anHttpRequest.open("GET", aUrl, true);
        anHttpRequest.setRequestHeader('content-type', 'application/json');
        anHttpRequest.setRequestHeader('Access-Control-Allow-Headers', '*');
        anHttpRequest.setRequestHeader('Access-Control-Allow-Origin', '*');
        anHttpRequest.send(payload);
    }
    else if ("POST" == aMethod)
    {
        anHttpRequest.open("POST", aUrl , true);
        anHttpRequest.setRequestHeader("content-type", "application/json");
        anHttpRequest.setRequestHeader('Access-Control-Allow-Headers', '*');
        anHttpRequest.setRequestHeader('Access-Control-Allow-Origin', '*');
        anHttpRequest.send(payload);
    }
    };
   };
在python wsgi中,我试图将post数据解码为:

def handlePost(environ, start_response):
    request_body_size = int(environ.get('Content-Length', 0))
    request_body = environ['wsgi.input'].read(request_body_size)
    data = json.loads(request_body)     
    what = data['what']

但它给我的错误是加载json失败

这些类似的帖子可能会有所帮助:你确定你的JS代码没有问题吗?