微信公众号搜"智元新知"关注
微信扫一扫可直接关注哦!

使用 React fetch 给出了 pyramid.exceptions.PredicateMismatch:找不到资源

如何解决使用 React fetch 给出了 pyramid.exceptions.PredicateMismatch:找不到资源

我正在编写一个 React Web 客户端,它使用 REST API 与 Python Pyramid 后端进行通信。问题是只有 POST 和 GET 方法有效。所有其他在服务器上给出错误。另外我只能使用“接受”标头,所有其他都给出相同的错误

pyramid.exceptions.PredicateMismatch:找不到资源。

它在 Android 应用程序中运行良好,在浏览器模式下使用 CURL、使用 Postman 和 REST API 插件。但是对于 React 中的 fetch 命令,它不起作用。

服务器端:

config.add_route('admin_language_api','/api/admin/language')
def allow_CORS(config,log):
    def add_cors_headers_response_callback(event):
        def cors_headers(_,response):
            response.headers.update({
                'Access-Control-Allow-Origin': '*','Access-Control-Allow-Methods': 'POST,GET,DELETE,PUT,OPTIONS','Access-Control-Allow-Headers': 'Origin,Content-Type,Accept,Authorization','Access-Control-Allow-Credentials': 'true','Access-Control-Max-Age': '1728000'
            })
        event.request.add_response_callback(cors_headers)
    config.add_subscriber(add_cors_headers_response_callback,NewRequest)
@view_config(route_name='admin_language_api',request_method='DELETE',renderer='json')
# @auth.require_api_auth
def delete_language(request):

反应端:

export async function PServLanguagePost(languageObj) {
    const data = await fetch('http://localhost:6543/api/admin/language',{
        method: 'DELETE',body: JSON.stringify(languageObj)
    });
    return await data.json();
}

服务器错误

2021-05-07 04:53:34,041 INFO  [pyramid_debugtoolbar:287][waitress-3] Squashed pyramid.exceptions.PredicateMismatch at http://localhost:6543/api/admin/language      
traceback url: http://localhost:6543/_debug_toolbar/31383138393039313238303136/exception

金字塔转储:

请求: <Request at 0x228d20ffca0 OPTIONS http://localhost:6543/api/admin/language>

属性{'environ': {'REMOTE_ADDR': '127.0.0.1','REMOTE_HOST': '127.0.0.1','REMOTE_PORT': '56013','REQUEST_METHOD': 'OPTIONS','SERVER_PORT': '6543','SERVER_NAME': 'waitress.invalid','SERVER_SOFTWARE': 'waitress','SERVER_PROTOCOL': 'HTTP/1.1','SCRIPT_NAME': '','PATH_INFO': '/api/admin/language','QUERY_STRING': '','wsgi.url_scheme': 'http','wsgi.version': (1,0),'wsgi.errors': <_io.TextIOWrapper name='<stderr>' mode='w' encoding='utf-8'>,'wsgi.multithread': True,'wsgi.multiprocess': False,'wsgi.run_once': False,'wsgi.input': <_io.BytesIO object at 0x00000228D35C7040>,'wsgi.file_wrapper': <class 'waitress.buffers.ReadOnlyFileBasedBuffer'>,'wsgi.input_terminated': True,'HTTP_HOST': 'localhost:6543','HTTP_USER_AGENT': 'Mozilla/5.0 (Windows NT 10.0; Win64; x64; rv:88.0) Gecko/20100101 Firefox/88.0','HTTP_ACCEPT': '*/*','HTTP_ACCEPT_LANGUAGE': 'en-US,en;q=0.5','HTTP_ACCEPT_ENCODING': 'gzip,deflate','HTTP_ACCESS_CONTROL_REQUEST_METHOD': 'DELETE','HTTP_REFERER': 'http://localhost:3000/','HTTP_ORIGIN': 'http://localhost:3000','HTTP_CONNECTION': 'keep-alive','waitress.client_disconnected': <bound method HTTPChannel.check_client_disconnected of <waitress.channel.HTTPChannel connected 127.0.0.1:56013 at 0x228d33c93d0>>,'webob._parsed_cookies': ({},''),'CONTENT_LENGTH': '0','webob.is_body_seekable': True,'webob._parsed_query_vars': (GET([]),'webob._cache_control': ('',<CacheControl ''>)},'registry': pyramid.registry.Registry({}),'invoke_subrequest': <bound method Router.invoke_subrequest of <pyramid.router.Router object at 0x00000228D1D07610>>,'pdtb_id': '32333734333436323130343634','finished_callbacks': collections.deque([]),'_headers': <webob.headers.EnvironHeaders object at 0x00000228D33C9B80>,'pdtb_sqla_queries': [],'request_iface': <InterfaceClass pyramid.request.admin_language_api_IRequest>,'response_callbacks': collections.deque([]),'matchdict': {},'matched_route': <pyramid.urldispatch.Route object at 0x00000228D358EA00>,'root': <pyramid.traversal.DefaultRootFactory object at 0x00000228D33C9C10>,'view_name': '','subpath': (),'traversed': (),'virtual_root': <pyramid.traversal.DefaultRootFactory object at 0x00000228D33C9C10>,'virtual_root_path': (),'locale_name': 'en','exception': <PredicateMismatch at 0x228d35d8160 404 Not Found>,'exc_info': (<class 'pyramid.exceptions.PredicateMismatch'>,<PredicateMismatch at 0x228d35d8160 404 Not Found>,<traceback object at 0x00000228D3448C40>)}

版权声明:本文内容由互联网用户自发贡献,该文观点与技术仅代表作者本人。本站仅提供信息存储空间服务,不拥有所有权,不承担相关法律责任。如发现本站有涉嫌侵权/违法违规的内容, 请发送邮件至 dio@foxmail.com 举报,一经查实,本站将立刻删除。