Apitester成功时从reactjs调用调用Webservice失败

时间:2019-07-20 12:29:32

标签: python reactjs

从我的React代码中获取登录名。但是所有后续的reactjs提取都会失败...

以下代码将打印此内容... get_info失败TypeError:无法获取

  {
    const self = this;
    fetch('http://xx.xx.xx.xx/api/fold/', {
        method: 'post',
        headers: {
            'Content-Type': 'application/json; charset=UTF-8',
            'Cookie': 'sessionid=asda43a7654sd6das4asd5as6d4ad4; HttpOnly; Path=/; SameSite=Lax',
        },
        body: JSON.stringify({
            function: 'get_info',
            fold_id: '1'
        })
    })
    .then(function(response){
        return response.json();
    })
    .then(function(myJson) {
        self.setState({ answers:myJson.data });
    })
    .catch(error => {
      console.log("get_info failed "+error);
    });
  }

反应获取失败,并显示TypeError。 但是从apitester.com或python客户端调用的同一个webapi可以成功...

request body:
{ "function": "get_info"}

request header:
POST /api/fold/ HTTP/1.1
Host: xx.xx.xx.xx
Accept: */*
User-Agent: Mozilla/5.0 (compatible; Rigor/1.0.0; http://rigor.com)
Cookie: sessionid=asda43a7654sd6das4asd5as6d4ad4; HttpOnly; Path=/; SameSite=Lax
Content-Type: application/json
Content-Length: 31


response header:
HTTP/1.1 200 OK
Date: Sat, 20 Jul 2019 11:57:26 GMT
Server: WSGIServer/0.2 CPython/3.7.3
Content-Type: application/json
X-Frame-Options: SAMEORIGIN
Content-Length: 1257
Vary: Origin, Cookie

response:
{"success": true, "data": [{"WERWEREYRTRTYRTY"}]}

已使用python和django实现了Web服务 在设置中进行了以下设置。py

CORS_ORIGIN_ALLOW_ALL = True
ALLOWED_HOSTS = ['xx.xx.x.x]', 'localhost']

如何使React Fetch成功? 谢谢

0 个答案:

没有答案