如何将PHP数组传递给JavaScript变量?
我有以下JavaScript将参数发送到PHP文件:如何将PHP数组传递给JavaScript变量?,javascript,php,json,Javascript,Php,Json,我有以下JavaScript将参数发送到PHP文件: function getOutput() { $.ajax({ url:'myPHPFile.php', data:{APIKey:$APIKey,Password:$APIPass,Alias:$Alias,DataCenter:$DataCenter}, type:'POST', complete: function (response) { $('#output').html(response.responseText); }, err
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;
}`
<a href="#" onclick="return getOutput();"> test </a>
将以下HTML更改为PHP文件的输出:
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;
}`
<a href="#" onclick="return getOutput();"> test </a>
这里是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;
Ajax调用(通常)是异步的,这意味着返回response.responseText代码>将立即执行,甚至会引发与未定义的响应相关的错误
您将在ajax调用的complete
事件中得到响应,并在其中继续执行脚本。jQuery将自动解析JSON,response
将成为结果对象
另一方面,PHP脚本应该只打印json_encode()
的结果,而不打印任何其他内容,以使响应成为有效的json。Ajax调用(通常)是异步的,这意味着返回response.responseText代码>将立即执行,甚至会引发与未定义的响应相关的错误
您将在ajax调用的complete
事件中得到响应,并在其中继续执行脚本。jQuery将自动解析JSON,response
将成为结果对象
另一方面,PHP脚本应该只打印json_encode()
的结果,而不打印其他内容,以使响应成为有效的json。将json作为ajax响应发送,或者在生成页面时将其嵌入页面:var somevar=代码>可能的重复:将json作为ajax响应发送,或者在生成页面时将其嵌入页面:var somevar=代码>可能重复的