0

Sorry i see that here is heap of similar questions, but i will be dare to ask the same. I'm sending javascript two dimensional array to PHP script and then it saved to database. I'm using JSON.stringify and have such string in the database

[["babaguliala","979485","xlopa kupa 12"],["didoboroda","222455","nemadoma 12"]]

But when i try to retrieve this array and parse it i got an error. This is part of PHP fubction that sends array to javascript:

$organizations = get_user_meta($current_user_id, 'organizations');
echo  $organizations;

But when i try to parse this data i'm getting error:

console.log( 'response: ' + response );    // response: Array      
organizations =JSON.parse(response); // Error

Error detail is here:

Uncaught SyntaxError: Unexpected token A in JSON at position 0
    at JSON.parse (<anonymous>)
    at Object.success ((індекс):562:24)
    at c (jquery.min.js?ver=3.6.0:2:28327)
    at Object.fireWith [as resolveWith] (jquery.min.js?ver=3.6.0:2:29072)
    at l (jquery.min.js?ver=3.6.0:2:79901)
    at XMLHttpRequest.<anonymous> (jquery.min.js?ver=3.6.0:2:82355)

But when it try thiscode enter into console:

const ajaxResponse = '[["babaguliala","979485","xlopa kupa 12"],["didoboroda","222455","nemadoma 12"]]'
const arr = JSON.parse(ajaxResponse)

I'm getting nice response and parsing went right.

arr[0] //  ["babaguliala", "979485", "xlopa kupa 12"] (3)
arr[1] //  ["didoboroda", "222455", "nemadoma 12"] (3)

Sorry but i do not understand what this behaviour mean. Could someone explain ?

2
  • have a look in the database and see how it is saved and make sure that is a valid JSON. The JSON is most probably escaped or modified when inserted in the DB Commented Jul 26, 2022 at 20:15
  • Try exiting after your response. Check out wp_ajax_ hooks. This question doesn't seem to be specific to WordPress. Commented Jul 26, 2022 at 22:55

1 Answer 1

0

Your PHP code shows:

$organizations = get_user_meta($current_user_id, 'organizations');

The signature for this function is: get_user_meta( int $user_id, string $key = '', bool $single = false )

Note the last argument. When you used this function, you omitted the last argument; therefore, the default was used. The default is false meaning that the value will be returned as a PHP array. This is because it is actually possible to store several meta values with the same key; when doing so, each value acts like an element in an array. If you are not storing values in this way, you should be using true in the last argument so that get_user_meta does not return a PHP array.

To repeat, when the $single argument is true it means that get_user_meta returns a single value and not a PHP array. When $single is false, it means that your JSON string (the whole thing) is returned as the first element of a PHP array. In this case, to return organizations back to the client, you need to do this:

echo $organizations[0];

On the other hand... It might be a good idea to consider using the "application/json" MIME type when returning JSON content to the browser. You can tell the client that you are returning a JSON string. By doing so, you don't have to use JSON.parse() on the client side. The content received client-side will already be a JSON object.

Assuming the string from the database is already in valid JSON format, you could try using:

$organizations = get_user_meta($current_user_id, 'organizations', true); // true means return a single string, not a PHP array
header('Content-Type: application/json');
echo $organizations;

then, the client-side JavaScript can use:

var organizations = response;
2
  • Thank you man for this detailed explanation. I found answer by myself. And yes you're right . it's finally working. Commented Jul 29, 2022 at 22:10
  • You're welcome. For the benefit of others who find your question in their search results, please indicate that a correct answer was provided. It will help them to know that an answer is available to their own questions (and reduce the "heap of similar questions" you mention). :-) Thanks. Commented Jul 30, 2022 at 17:42

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.