Przesłanie tablicy 2D z PHP do JS

0

Witam.
W jaki sposób prawidłowo przesłać tablicę 2D z pliku PHP do pliku JS?
Próbowałem wykonać to w poniższy sposób jednak przy wykonywaniu otrzymuję błąd:
Uncaught SyntaxError: Unexpected token o.

tableData.php

<?php
	$array = array(array('1', 'WW', '36'), array('2', 'WQ', '65'));
	
	echo json_encode($array);
?>

table.js

function showAdditionalInformations(show)
{
	var excludedKeywords = [];
	
	if (!show) excludedKeywords.push('AdditionalInformations');
	
	 $.ajax({		 
	      url:'../php/tableData.php',
	      type: "POST",
	      data: {
	    	  excludedKeywords: excludedKeywords
	      },
	      complete: function (response) {
	    	 
	    	  console.log(response);

	    	  var data = $.parseJSON(response);
	    	  
	  	  console.log(data);
	      }
	 })
}

user image

1

Ehm, response.responseText, przecież nawet wcześniej wykonałeś console.log(response); i tam to widać.

0

Po prawidłowym wywołaniu funkcji otrzymałem dane w postaci:
[Array[3], Array[3]]

W jaki sposób mogę wywołać funkcję dla każdej tablicy?
Próbowałem w poniższy sposób, jednak wartością dataLenght jest undefinied.

function showAdditionalInformations(show)
{
	var excludedKeywords = [];
	
	if (!show) excludedKeywords.push('AdditionalInformations');
	
	 $.ajax({		 
	      url:'../php/tableData.php',
	      type: "POST",
	      data: {
	    	  excludedKeywords: excludedKeywords
	      },
	      complete: function (response) {
	    	 
	    	  console.log(response);

	    	  var data = $.parseJSON(response.responseText);
	    	  
	  	   	  console.log(data);

	  	   	  var dataLenght = data.length;
	  	   	 
	  	   	  console.log(dataLenght);

	  	   	  for (var index = 0; index < dataLenght; ++index)
  	   		  {
		  	   	  console.log(data[index]);

	  	   		  addRow('keywords', data[index]); 	    
  	   		  }
	  
	      }
	 })
}
1

Co zwraca Twój kod?
Plus to może być bardziej przydatne: https://developer.mozilla.org/pl/docs/Web/JavaScript/Referencje/Obiekty/Array/forEach

Btw, nie lenght, a length.

0

Kod wyświetlał tylko zawartość zmiennej dataLenght (literówka), pętla nie wykonywała się.

Pętla forEach rozwiązała problem.
Dziękuję za pomoc.

1 użytkowników online, w tym zalogowanych: 0, gości: 1