所以我使用PHP进行服务器端脚本编写,php接收一个字符串作为HTTP GET请求,最后,它回显一个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中获取此对象作为在Javascript中使用的var?
$(document).ready(function() {
$('#question').on('submit', function() {
event.preventDefault();
$.get("durhamServer.php", function() {
$('#response1').load("durhamServer.php", $('#request').serialize());
});
});
});
此Javascript代码向HTML页面显示php echo的值。我怎样才能将这个值作为javascript获取,因为我想直接操作客户端
答案 0 :(得分:0)
这样的事情:
$.ajax({
type: "GET",
url: url,
success: function(response) {
var variables = response;
}
});
答案 1 :(得分:0)
首先,您将使用JQuery的Ajax来获取您的PHP URL
http://api.jquery.com/jquery.ajax/
success
您的“回音”php内容将由error
函数(或404
处理,如果有任何错误,例如500
,success
等)< / p>
因此,您的Function( Anything data, String textStatus, jqXHR jqXHR )
函数应该对来自服务器的数据执行某些操作。从JQuery文档中,以下是JQ如何安排从服务器传入的数据:
data
因此,您的回显php数据包含在第一个参数success : function(myPhpData, textStatus, jqXHR){
// your echoed php data is now on myPhpData and you can do whatever you want with it
}
success
请记住,Ajax调用是异步的: public Student(String name, String emailID)
{
super(name,emailID);
attendance = 0;
}
函数只有在服务器响应后才会执行。如果您在ajax调用之后编写任何内容,则可能存在竞争条件的风险 - 因此请学习如何控制JavaScript代码的流程。