-1

When I do a POST method to my PHP backend, I receive this response, but is there a way to just grab the object from the response.data?

<br />
<b>Warning</b>:  Undefined array key "session_id" in <b>/home/cs/experimetns/RunnerMaps/RunnerMapPrototype/api/index.php</b> on line <b>9</b><br />
<br />
<b>Notice</b>:  session_start(): Ignoring session_start() because a session is already active in <b>/home/cs/experimetns/RunnerMaps/RunnerMapPrototype/api/index.php</b> on line <b>11</b><br />
{"success":"Login successful","role":"admin","session_id":"4kj0sflsnevp520ebitdq5iev1"}

When I do a respone.data[0], it results to a "<" and I am not able to do response.data["role"] since it sees it as a string.

Here is my Axios call using React.

await Axios.post(currentUrl, params).then((response) => {
  if (response.data["error"]) {
    alert(response.data["error"]);
  } else {
    //
    console.log(response.data);
    localStorage.setItem("session_id", response.data["session_id"]);
    localStorage.setItem("role", response.data["role"]);
    if (response.data["role"] == "admin") {
      navigate("/admin");
    } else {
      navigate("/user");
    }
  }
});

There isn't any way to touch the PHP code, so I can't just check if I have a session. This is what I will always receive, but I only want

{"success":"Login successful","role":"admin","session_id":"4kj0sflsnevp520ebitdq5iev1"}
Peter Mortensen
  • 30,030
  • 21
  • 100
  • 124
lga2
  • 23
  • 4
  • 2
    You should not respond with mixed HTML and JSON. Fix the errors on your PHP side so they don't pollute the response – Phil May 09 '22 at 00:00
  • Unfortunately I do not have access to the PHP side. @Phil – lga2 May 09 '22 at 00:45
  • 2
    Try setting `withCredentials: true`. It's not clear from your question if the request is cross-origin but you may need that in order to pass the session cookie... `Axios.post(currentUrl, params, { withCredentials: true })`. You should also contact whoever maintains the server-side code and tell them about the error – Phil May 09 '22 at 00:53

0 Answers0