所以我使用PHP进行服务器端脚本编写,PHP接收一个字符串作为HTTPGET请求,最后,它返回一个JSON对象。
如何使用Javascript/JQuery将此JSON echo作为Javascript上的"var"?我想将这个JSON对象用于客户端脚本。
$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中使用的JSON对象。我如何从php中获得这个对象作为var在Javascript中使用?
$(document).ready(function() {
$('#question').on('submit', function() {
event.preventDefault();
$.get("durhamServer.php", function() {
$('#response1').load("durhamServer.php", $('#request').serialize());
});
});
});
此Javascript代码显示HTML页面的php回声的值。我如何在javascript上获得这个值作为var,因为我想直接在客户端操作
类似这样的东西:
$.ajax({
type: "GET",
url: url,
success: function(response) {
var variables = response;
}
});
首先,您将使用JQuery的Ajax来获取您的PHP URL
http://api.jquery.com/jquery.ajax/
$.ajax({
method: "GET",
url: <your php url>,
success : <a function to handle the success>,
error : <a function to handle any errors>
})
您的"回声"php内容将由success
函数(或error
,如果有任何错误,如404
、500
等)处理
因此,success
函数应该对来自服务器的数据进行处理。从JQuery文档中,以下是JQ如何安排来自服务器的数据混合:
Function( Anything data, String textStatus, jqXHR jqXHR )
因此,您的响应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代码的流。