0

I send an ajax request with jquery like this:

 $.post("ajax/myFile.php", {
    myValue: "test"
    }, function(response) {
      console.log($.parseJSON(response))
    })

content of myFile.php

myFunctionA();
myFunctionB();

function myFunctionA() {
   echo json_encode(array('error' => 0, 'message' => "Hello World"));
}


function myFunctionB() {
    echo json_encode(array('error' => 0, 'message' => "Hello again"));
}

my console.log result:

Uncaught SyntaxError: JSON.parse: unexpected non-whitespace character after JSON data at line 1 column 44 of the JSON data

how can I handle this? :/

1
  • 1
    It would help if you would include the actual JSON response you are getting. I'm guessing that the two individual echos are each outputting their own JSON, not a cohesive JSON response, so basically 2 JSON back to back, which isn't a valid JSON response. Commented Jan 5, 2023 at 17:44

1 Answer 1

0

You can't return multiple JSON values from your script. You need to combine them into a single value.

echo json_encode([myFunctionA(), myFunctionB()]);

function myFunctionA() {
   return array('error' => 0, 'message' => "Hello World");
}


function myFunctionB() {
    return array('error' => 0, 'message' => "Hello again");
}

This will return both results as elements of an array in JavaScript.

Sign up to request clarification or add additional context in comments.

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.