这是我的问题:我向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;
答案 0 :(得分:-1)
试试这个
$.ajax({
url : _url,
type : "GET",
dataType : "JSON",
error : function(){
console.log("Cannot bring the core fields");
},
success : function (response){
console.info( response );
});