Search code examples
parsingreduxreact-reduxfetch-apiredux-toolkit

What format does Redux Toolkit Query expect a response to be?


When I did the following:

import { useGetPostQuery } from "./store/postApi";
export function App() {
  const { data, isLoading, isFetching, isError, error } = useGetPostQuery(1);
  if (isError)
    return (
      <div>
        {error.status}
        <br />
        {error.data}
      </div>
    );
  if (isLoading) return <div>loading...</div>;
  return <div className={isFetching ? "posts--disabled" : ""}>{data}</div>;
}

I received an error:

PARSING_ERROR HELLO WORLD

Basically, I just want to return the string HELLO WORLD from the server. How should I avoid the parsing error?

A Code Sandbox for the example can be found here.


Solution

  • I guess it expects a JSON response.

    My PHP script:

    echo json_encode(array("data"=>file_get_contents("data".$_GET["id"].".txt")));