如何在Javascript/JQuery上获取PHP JSON编码的对象作为变量?
因此,我使用PHP编写服务器端脚本,PHP接收一个字符串作为HTTP GET请求,最后,它回显一个JSON对象 如何使用Javascript/JQuery将此JSON回音作为Javascript上的“var”获取?我想将此JSON对象用于客户端脚本如何在Javascript/JQuery上获取PHP JSON编码的对象作为变量?,javascript,php,jquery,json,Javascript,Php,Jquery,Json,因此,我使用PHP编写服务器端脚本,PHP接收一个字符串作为HTTP GET请求,最后,它回显一个JSON对象 如何使用Javascript/JQuery将此JSON回音作为Javascript上的“var”获取?我想将此JSON对象用于客户端脚本 $query = trim(strtolower($_GET["query"]), "?"); $stopList = array("much", "many", "the", "who", "what", "where", "when", "why
$query = trim(strtolower($_GET["query"]), "?");
$stopList = array("much", "many", "the", "who", "what", "where", "when", "why", "how", "a", "is", "which", "so", "were", "there", "this", "did", "was", "will", "are", "you", "do", "I", "it", "are", "can", "i", "he", "she", "you", "did");
$templateFile = fopen("templates.txt", "r");
$templateList = array();
while(!feof($templateFile)) {
array_push($templateList, fgets($templateFile));
}
fclose($templateFile);
$index = rand(0, count($templateList) - 1);
$template = $templateList[$index];
function makeTemplate($template, $subject, $stopList) {
$listWords = explode(" ", $subject);
$goodList = array_diff($listWords, $stopList);
$answer = implode(" ", $goodList);
$response = str_replace('#word', $answer, $template);
return $response;
}
function containsUnwantedSymbol($haystack) {
return substr($haystack, 0 , 1) === "#" || substr($haystack, 0, 1) === "@" || substr($haystack, 0, 4) === "http" || substr($haystack, 0, 5) === "https";;
}
require_once("TwitterAPIExchange.php");
$settings = array(
"oauth_access_token" => "OAUTH_ACCESS_TOKEN_HERE",
"oauth_access_token_secret" => "OAUTH_ACCESS_TOKEN_SECRET_HERE",
"consumer_key" => "CONSUMER_KEY_HERE",
"consumer_secret" => "CONSUMER_SECRET_HERE"
);
$url = "https://api.twitter.com/1.1/search/tweets.json";
$requestMethod = "GET";
$getField = "?q=".$query;
$twitter = new TwitterAPIExchange($settings);
$response = $twitter->setGetfield($getField)
->buildOauth($url, $requestMethod)
->performRequest();
$details = json_decode($response, true);
$tweet = $details['statuses'][0]['text'];
$list = explode(" ", $tweet);
$against = array();
for($item = 0; $item < count($list); $item++) {
if($list[$item] == "RT") {
unset($list[$item]);
}
}
for($item = 0; $item < count($list); $item++) {
if(containsUnwantedSymbol($list[item]) || containsUnwantedSymbol($list[$item]) || containsUnwantedSymbol($list[$item])) {
unset($list[$item]);
}
}
$tweet = implode(" ", $list);
$dreet = makeTemplate($template, $query, $stopList);
$clauses = array("durhamResponse" => $dreet, "twitterResponse" => $tweet);
shuffle($clauses);
$json_clauses = json_encode($clauses);
echo $json_clauses;
此Javascript代码显示HTML页面的php回显值。如何在javascript上以var的形式获取此值,因为我要直接在客户端操作此值
$.ajax({
type: "GET",
url: url,
success: function(response) {
var variables = response;
}
});
首先,您将使用JQuery的Ajax获取PHP URL 因此,您的回显php数据包含在第一个参数中,
data
success : function(myPhpData, textStatus, jqXHR){
// your echoed php data is now on myPhpData and you can do whatever you want with it
}
记住Ajax调用是异步的:
success
函数将仅在服务器响应后执行。如果您在ajax调用后编写任何内容,您可能会面临竞争条件的风险——因此,请学习如何控制JavaScript代码流。JSON编码的数据将从ajax调用函数传回。请向我们展示进行AJAX调用的JS/jQuery代码,以便我们能够为您提供更好的答案,将PHP传递的数据转换回JS中的变量。如何调用PHP脚本?使用ajax还是直接使用?通过添加javascript,您可以很容易地实现这两种方法,javascript将显示回显的PHP值到HTML页面。如何将该值作为var获取?我假设var变量是存储php结果的javascript var?
$.ajax({
method: "GET",
url: <your php url>,
success : <a function to handle the success>,
error : <a function to handle any errors>
})
Function( Anything data, String textStatus, jqXHR jqXHR )
success : function(myPhpData, textStatus, jqXHR){
// your echoed php data is now on myPhpData and you can do whatever you want with it
}