【发布时间】:2014-11-29 08:11:30
【问题描述】:
这是我的问题:我向 WordPress PHP 函数发出 ajax 请求。奇怪的是:这个响应是作为错误返回的。
但在错误日志中,responseText 是请求的数据(JSON 为字符串),状态为 200,statusText 为“OK”。
这是请求:
$.ajax({
type: "GET",
dataType: "json",
url: ajaxurl,
data: { action:'my_wp_function',username: settings.username, list: settings.list, hashtag: settings.hashtag, count: settings.count, exclude_replies: settings.hideReplies, lastID:settings.lastID }
}).done(function( res ) {
}).error(function(err) { console.log(err); }); // err.responseText = the requested data ...
如果一切正常,为什么检测到这个请求好像发生了错误?
编辑,这是我要求的 PHP:
<?php
require_once("twitteroauth/twitteroauth.php");
require_once('config.php');
if (CONSUMER_KEY === '' || CONSUMER_SECRET === '' || CONSUMER_KEY === 'CONSUMER_KEY_HERE' || CONSUMER_SECRET === 'CONSUMER_SECRET_HERE') {
echo 'You need a consumer key and secret keys. Get one from <a href="https://dev.twitter.com/apps">dev.twitter.com/apps</a>';
exit;
}
$username = filter_input(INPUT_GET, 'username', FILTER_SANITIZE_SPECIAL_CHARS);
$number = filter_input(INPUT_GET, 'count', FILTER_SANITIZE_NUMBER_INT);
$exclude_replies = filter_input(INPUT_GET, 'exclude_replies', FILTER_SANITIZE_SPECIAL_CHARS);
$list_slug = filter_input(INPUT_GET, 'list_slug', FILTER_SANITIZE_SPECIAL_CHARS);
$hashtag = filter_input(INPUT_GET, 'hashtag', FILTER_SANITIZE_SPECIAL_CHARS);
$lastID = filter_input(INPUT_GET, 'lastID', FILTER_SANITIZE_SPECIAL_CHARS);
if(CACHE_ENABLED) {
// Generate cache key from query data
$cache_key = md5(
var_export(array($username, $number, $exclude_replies, $list_slug, $hashtag), true) . HASH_SALT
);
$cache_path = dirname(__FILE__) . '/cache/';
foreach (glob($cache_path . '*') as $file) {
if (filemtime($file) < time() - CACHE_LIFETIME) {
unlink($file);
}
}
if(file_exists($cache_path . $cache_key)) {
header('Content-Type: application/json');
echo file_get_contents($cache_path . $cache_key);
exit;
}
}
function getConnectionWithToken($cons_key, $cons_secret, $oauth_token, $oauth_secret)
{
$connection = new TwitterOAuth($cons_key, $cons_secret, $oauth_token, $oauth_secret);
return $connection;
}
// Connect
$connection = getConnectionWithToken(CONSUMER_KEY, CONSUMER_SECRET, ACCESS_TOKEN, ACCESS_SECRET);
// Get Tweets
if (!empty($list_slug)) {
$params = array(
'owner_screen_name' => $username,
'slug' => $list_slug,
'per_page' => $number
);
$url = '/lists/statuses';
}
else if($hashtag) {
if($lastID){
$params = array(
'max_id' => $lastID,
'count' => 100,
'q' => '#'.$hashtag.' filter:images',
'include_entities' => true
);
}
else {
$params = array(
'count' => 100,
'q' => '#'.$hashtag.' filter:images',
'include_entities' => true
);
}
$url = '/search/tweets';
} else {
$params = array(
'count' => $number,
'exclude_replies' => $exclude_replies,
'screen_name' => $username
);
$url = '/statuses/user_timeline';
}
// $rawTweets = $connection->get($url, $params);
// $tweets = array();
$tweets = $connection->get($url, $params);
// Return JSON Object
header('Content-Type: application/json');
$tweets = json_encode( $tweets);
if(CACHE_ENABLED) file_put_contents($cache_path . $cache_key, $tweets);
echo $tweets;
【问题讨论】:
-
控制台到底说了什么?
-
你能告诉我你的 php 代码的一部分,然后做接下来的几件事,从请求中删除 dataType,建立你想要在请求之前发送的数据和路径对象到数据选项,像这样:数据:对象,作为回答,请确保您解析您的 json 并在 php 中使用 json_encode
-
是的,PHP 代码返回 (echo) 一个 json 变量 (json_encode)。我尝试不使用 dataType:它是相同的(作为错误返回)。
-
你能复制/粘贴控制台输出吗,你可以用星号替换任何私人数据。
-
我粘贴了控制台输出:我从 responseText 中删除了很多数据(因为它太大了,所以请注意它是一个有效的 JSON 对象数组)。