React Native RTK查询,其中结果不可进行JSON解析

yptwkmov  于 2023-01-09  发布在  React
关注(0)|答案(2)|浏览(145)

我这样做是为了学习React工具包:

import { createApi, fetchBaseQuery } from "@reduxjs/toolkit/query/react";

export const getIPv4AddressApi = createApi({
  reducerPath: "getIPv4Address",
  baseQuery: fetchBaseQuery({
    baseUrl: "https://ip4.8n1.org/",
  }),
  endpoints: builder => ({
    getIPv4Address: builder.query<string, void>({
      query: () => "",
    }),
  }),
});

export const { useGetIPv4AddressQuery } = getIPv4AddressApi;

运行时,我收到错误

{"status":"PARSING_ERROR","originalStatus":200,"data":"xxx.xxx.xxx.xxx\n","error":"SyntaxError: Unexpected token . in JSON at position 6"}

我怎样才能返回整个响应而不尝试JSON解析呢?非常感谢

pkwftd7m

pkwftd7m1#

请参见解析响应

endpoints: builder => ({
    getIPv4Address: builder.query<string, void>({
      query: () => "",
      responseHandler: "text"
    }),
  }),
5gfr0r5j

5gfr0r5j2#

而不是离开查询:作为空字符串,您可以指定数据格式。下面是一个示例:enter image description here

相关问题