我的axios调用如下:
export function onixProductTitleByISBN(token, ISBN) {
const url = `${ONIX_URL}product-title-by-isbn/${ISBN}`;
let obj = {Authorization:`${token}`}
return dispatch => {
dispatch({type: ONIX_PRODUCT_TITLE_LOADING, isLoaded: false});
return axios.get(url, {headers:obj, followAllRedirects: true})
.then(({data}) =>{
dispatch({type: ONIX_PRODUCT_TITLE_SUCCESS, data})
} )
.catch((err) => dispatch({type: ONIX_PRODUCT_TITLE_FAILURE, isLoaded: true}))
}
};
这是生成的api调用: http://192.168.1.149:8000/api/product-title-by-isbn/9780020100607 如果我将该调用放入Postman,则可以获取该API以返回该值。
当我使用Axios调用运行相同的调用时,它返回:
301永久移动
在local.conf的nginx中,我们有以下代码:
# first we declare our upstream server, which is our Gunicorn application
upstream onix-api-server {
# docker will automatically resolve this to the correct address
# because we use the same name as the service: "onix-api"
server onix-api:8000;
}
# now we declare our main server
server {
listen 80;
location / {
# everything is passed to Gunicorn
proxy_pass http://onix-api-server;
proxy_set_header X-Forwarded-For $proxy_add_x_forwarded_for;
proxy_set_header Host $host;
proxy_redirect off;
if ($request_method = 'OPTIONS') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
#
# Custom headers and headers various browsers *should* be OK with but aren't
#
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
#
# Tell client that this pre-flight info is valid for 20 days
#
add_header 'Access-Control-Max-Age' 1728000;
add_header 'Content-Type' 'text/plain; charset=utf-8';
add_header 'Content-Length' 0;
return 204;
}
if ($request_method = 'POST') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
if ($request_method = 'GET') {
add_header 'Access-Control-Allow-Origin' '*';
add_header 'Access-Control-Allow-Methods' 'GET, POST, OPTIONS';
add_header 'Access-Control-Allow-Headers' 'Authorization,DNT,User-Agent,X-Requested-With,If-Modified-Since,Cache-Control,Content-Type,Range';
add_header 'Access-Control-Expose-Headers' 'Content-Length,Content-Range';
}
}
location /static/ {
alias /static/;
}
}
与其返回值,不如调用同一路径并在其末尾添加一个/,然后给出404 not found错误
http://192.168.1.149:8000/api/product-title-by-isbn/9781640532632/
我不确定为什么要永久移动它,然后在末尾用/调用相同的路径,这似乎与我们使用gunicorn并将其传递给nginx有关。
但是我不确定这是否是失败和找不到路径的原因。我希望第一个路径返回Postman给出的值,但是我不确定为什么浏览器不返回数据。任何帮助表示赞赏。