Receiving Data from PHP script to JS

I was hoping for some help on how to handle these object in case of errors. I want to ensure each object has data that I can process before I send it to the functions. What I am finding that in the case something is up with object1 execution goes to catch statement even thought I could have doTwo function run normally.

  try {
    const response = await fetch('my_script.php');
    const data = await response.json();
    let object_1 = [JSON.parse(data['first'][0]), JSON.parse(data['first'][1])];
    let object_2 = [JSON.parse(data['second'][0]), JSON.parse(data['second'][1])];
    let object_3 = [JSON.parse(data['third'][0]), JSON.parse(data['third'][1])];
    let object_4 = [JSON.parse(data['forth'][0]), JSON.parse(data['forth'][1])];
    doOne(object_1, object_2);
    doTwo(object_3, object_4);

  }
  catch (error) {
  console.log("catch");
  console.error('This is the error -> :', error)
  }

You… shouldnt need to JSON.parse anything here, unless for some reason your json from the server was carrying json strings inside it?

yes it is the string

So you put JSON data strings… inside your JSON response data?

Why didnt you just… put the data in the response to begin with?

This is how I sent it from PHP and this is data from 4 different API calls.

$result = array(
  'first'  => array ($response1[0],$response1[1]),
  'second' => array ($response2[0],$response2[1]),
  'third'  => array ($response3[0],$response3[1]),
  'forth'  => array ($response4[0],$response4[1])
);
echo json_encode($result); 

So $response1[0] is a JSON string, and $response1[1] is a JSON string, etc.?

Okay… i’m… not sure if json_encode is going to process that cleanly… what’s it look like when your server sends it out?

yes and $response1[1] is http code which i can use for error checking. everything works as it should and I am mostly curious about how to handle the part of the code that I shared above. I was hoping to still be able to do something with other objects in case one has error

Wait… if response1[1] is an HTTP code, not a JSON string, and you try to JSON.parse it, it will throw an error. This is why i specifically asked you if all of those are JSON strings…are they?

it is a number as string. is it not?

  $response = curl_exec($ch);
  $httpCode = curl_getinfo($ch, CURLINFO_HTTP_CODE);

  // Check for errors
  if ($httpCode >= 400) {
      // Handle error, for example, log it or throw an exception
      return array (false, $httpCode);
  }

  // Close cURL session
  curl_close($ch);
 
  //return data from the API call as a string
  return array ($response, $httpCode);

No, a JSON string defines an object. "{\"thisis\":\"an object\"}" is a JSON string. "400" is not a JSON string.

Ok make sense. Thank you

ok have to go get little refresher…
java script calls php script which makes 4 api calls

api calls returns the string and http code as an integer
I put those two into an array and each of these 4 arrays are put into associative array in PHP and send it out via echo statement echo json_encode()

Does that make sense?

This topic was automatically closed 91 days after the last reply. New replies are no longer allowed.