1

I have the following JavaScript that sends parameters to a PHP file:

function getOutput()
{
$.ajax({
url:'myPHPFile.php',
data:{APIKey:$APIKey,Password:$APIPass,Alias:$Alias,DataCenter:$DataCenter},
type:'POST',
complete: function (response) {
$('#output').html(response.responseText);
},
error: function ()
{
$('#output').html('Bummer: there was an error!');
}
});
return response.responseText;
}`

Which changes the following HTML to the output of the PHP file:

<a href="#" onclick="return getOutput();"> test </a>

Here is the PHP

<?php
//  echo nl2br("\nIntializing api.php \n");
// DATA SECTION
$APIKey = $_POST["APIKey"];
$APIPass = $_POST["Password"];
$AccountAlias = $_POST["Alias"];
$dataCenter = $_POST["DataCenter"];

$data = array(
  "APIKey" => $APIKey,
  "Password" => $APIPass,   
);

$url_send = 'https://api.ctl.io/REST/Auth/Logon/';
$json_data = json_encode($data);

function sendPostData($url, $post, $cook = null){
//  echo "Beginning sendPostData($url, $post, $cook)";

$ch = curl_init($url);
$headers= array('Accept: application/json','Content-Type: application/json'); 
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_HTTPHEADER, $headers);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, "POST");  
curl_setopt($ch, CURLOPT_POSTFIELDS,$post);
curl_setopt($ch, CURLOPT_FOLLOWLOCATION, 1);

if (!empty($cook))
  {
    curl_setopt($ch, CURLOPT_HTTPHEADER, array('Accept: application/json','Content-Type: application/json','Cookie:'.$cook));     
  }

$result = curl_exec($ch);
curl_close($ch);  // Seems like good practice
return $result; 
};
$myresult = sendPostData($url_send, $json_data);
//  print_r ($myresult);
$decodedresult = json_decode($myresult);
// print_r ($decodedresult);
'/reply-(.*?)-private/';
preg_match_all('/Tier3(.*?)path=/', $myresult, $matches); 
$cookies = array(); 

foreach($matches[0] as $item) 
        { 
          parse_str($item, $cookie); 
          $cookies = array_merge($cookies, $cookie); 
        }
$prefix = 'Tier3.API.Cookie=';
$cookie = implode(" ",$matches[0]);

// Call the customer server list
$data = array(
  'AccountAlias' => $AccountAlias,
  'Location' => $dataCenter
);
$data_url =     'https://api.ctl.io/REST/Server/GetAllServersForAccountHierarchy/';
$data_string = json_encode($data);

$dataResult = sendPostData($data_url,$data_string, $cookie);
print_r($dataResult);
return $dataResult;

`

How can I get the $dataResult PHP array into a javascript variable so I can parse it? It is a big JSON response from an API.

Thanks.

  • 2
    either send the json as the ajax response, or embed it in the page when you generate the page: `var somevar = ;` – Marc B Apr 26 '16 at 21:27
  • Possible duplicate of [Pass a PHP array to a JavaScript function](http://stackoverflow.com/questions/4885737/pass-a-php-array-to-a-javascript-function) – Sam Segers Apr 26 '16 at 23:31

1 Answers1

0

Ajax calls are (normally) asynchronous, this means that the return response.responseText; will be executed immediately and should even raise an error related to response being undefined.

You'll have the response in the complete event of the ajax call and is inside there where you should go on with the execution of the script. jQuery will parse the JSON automatically and response will be the resulting object.

At the other side, the PHP script should just print the result of json_encode() and nothing else in order for the response to be valid JSON.

Gabriel
  • 2,170
  • 1
  • 17
  • 20