RTK 查询指向 localhost 而不是 baseUrl

问题描述 投票:0回答:2

代码如下。我通过 Components/Pages/Scrap.tsx console.log 以下错误。

{status: 'PARSING_ERROR', originalStatus: 200, data: '<!DOCTYPE html>\n<html lang="en">\n  <head>\n    <met…uild` or `yarn build`.\n    -->\n  </body>\n</html>\n', error: `SyntaxError: Unexpected token '<', "<!DOCTYPE "... is not valid JSON`}
data: "<!DOCTYPE html>\n<html lang=\"en\">\n  <head>\n    <meta charset=\"utf-8\" />\n    <link rel=\"icon\" href=\"/favicon.ico\" />\n    <meta name=\"viewport\" content=\"width=device-width, initial-scale=1\" />\n    <meta name=\"theme-color\" content=\"#000000\" />\n    <meta\n      name=\"description\"\n      content=\"Web site created using create-react-app\"\n    />\n    <link rel=\"apple-touch-icon\" href=\"/logo192.png\" />\n    <!--\n      manifest.json provides metadata used when your web app is installed on a\n      user's mobile device or desktop. See https://developers.google.com/web/fundamentals/web-app-manifest/\n    -->\n    <link rel=\"manifest\" href=\"/manifest.json\" />\n    <!--\n      Notice the use of  in the tags above.\n      It will be replaced with the URL of the `public` folder during the build.\n      Only files inside the `public` folder can be referenced from the HTML.\n\n      Unlike \"/favicon.ico\" or \"favicon.ico\", \"/favicon.ico\" will\n      work correctly both with client-side routing and a non-root public URL.\n      Learn how to configure a non-root public URL by running `npm run build`.\n    -->\n    <title>React App</title>\n  <script defer src=\"/static/js/bundle.js\"></script></head>\n  <body>\n    <noscript>You need to enable JavaScript to run this app.</noscript>\n    <div id=\"root\"></div>\n    <!--\n      This HTML file is a template.\n      If you open it directly in the browser, you will see an empty page.\n\n      You can add webfonts, meta tags, or analytics to this file.\n      The build step will place the bundled scripts into the <body> tag.\n\n      To begin the development, run `npm start` or `yarn start`.\n      To create a production bundle, use `npm run build` or `yarn build`.\n    -->\n  </body>\n</html>\n"
error: "SyntaxError: Unexpected token '<', \"<!DOCTYPE \"... is not valid JSON"
originalStatus: 200
status: "PARSING_ERROR"
[[Prototype]]: Object

开发工具的网络选项卡中的一些调查显示 RTK查询似乎正在尝试查询 http://localhost:3000/scrape?url=https://somewebsite.com 而不是预期的终点 https://localhost:7125/scrape?url=https://somewebsite.com

api/scrape-api.ts

import { createApi, fetchBaseQuery } from '@reduxjs/toolkit/query/react';
import type { scrapeModel } from '../models/scrape-model';

export const scrapeApi = createApi({
    reducerPath: 'scrapeApi',
    baseQuery: fetchBaseQuery({ baseUrl: 'https://localhost:7125/' }),
    endpoints: builder => ({
        scrapeUrl: builder.query<scrapeModel, string>({
            query: url => `scrape?url=${url}`
        })
    })
});

export const { useScrapeUrlQuery } = scrapeApi;

商店/index.ts

import { configureStore } from '@reduxjs/toolkit';
import { setupListeners } from '@reduxjs/toolkit/dist/query';
import { scrapeApi } from '../api/scrape-api';
import scrapeReducer from './scrape';

export const store = configureStore({
    reducer: {
        [scrapeApi.reducerPath]: scrapeApi.reducer,
        scrape: scrapeReducer
    },
    middleware: getDefaultMiddleware => {
        return getDefaultMiddleware().concat(scrapeApi.middleware);
    }
});

setupListeners(store.dispatch);

export type RootState = ReturnType<typeof store.getState>;

组件/页面/Scrap.tsx

import { useEffect } from 'react';
import { useScrapeUrlQuery } from '../../api/scrape-api';

const Scrape = () => {
    const { data, error, isLoading } = useScrapeUrlQuery(
        'https://somewebsite.com'
    );

    useEffect(() => {
        if (error) {
            console.log(error);
        }
    }, [data, error, isLoading]);

    return (
        <div>
            <h1>I'm the scrape page</h1>
            {isLoading && <div>Loading...</div>}
        </div>
    );
};

export default Scrape;
reactjs localhost rtk-query
2个回答
0
投票

希望你已经解决了这个问题,我和你有同样的问题(GH讨论),也向后端发送了一个URL。

在我的例子中,请求发送到 localhost:5173(Vite 端口正在运行 React 应用程序)。

我的问题是发送原始 URL。我在我的 URL 中添加了一个 encodeURIComponent,之后请求被发送到我的 RTK api 中的指定 URL。


0
投票

在 api/scrape-api.ts 中将其更改为

scrapeUrl: builder.query<scrapeModel, string>({
query: url => `scrape?url=${encodeURIComponent(url)}`

})

© www.soinside.com 2019 - 2024. All rights reserved.