I’m using the following code to perfom a search on Twitter’s API :
$.post('lib/themattharris-tmhOAuth-38bd48b/search.php',
{q:'@something',
pages:'1'}
)
.done(function(xhr) {
try
{
var tweets = $.parseJSON(xhr.responseText);
}
catch (e)
{
showError('Error : '+e.message);
return;
}
if (tweets === null)
{
showError('Error : no tweets found');
return;
}
$('#timeline').text(tweets.results[0].text);
})
.fail(function(xhr, textStatus, errorThrown) {
showError('Error : ' + $.parseJSON(xhr.responseText).error);
})
.always(function() {
$('#load').removeAttr('disabled');
});
But “tweets” is always null. My search.php does the search and simply echoes the result. I’ve checked the JSON returned in jsonlint, no errors are found.
Any clues ?
EDIT here’s search.php :
require_once 'tmhOAuth.php';
require_once 'tmhUtilities.php';
require_once 'secrets.php';
session_start();
$tmhOAuth = new tmhOAuth(array(
'consumer_key' => $consumerKey,
'consumer_secret' => $consumerSecret,
'user_token' => $_SESSION['access_token']['oauth_token'],
'user_secret' => $_SESSION['access_token']['oauth_token_secret']
));
$p['q'] = $_POST['q'];
$p['pages'] = 1;
$p['include_entities'] = 'true';
$pages = intval($p['pages']);
$results = array();
$code = $tmhOAuth->request(
'GET',
'http://search.twitter.com/search.json',
$p,
false
);
if ($code !== 200) {
header("HTTP/1.1 500 Internal Server Error");
echo $tmhOAuth->response['response'];
return;
}
else
{
echo $tmhOAuth->response['response'];
}
This worked ^^;
I thought you were supposed to use
.responseText?EDIT : “The success callback function is passed the returned data, which will be an XML root element, text string, JavaScript file, or JSON object, depending on the MIME type of the response.“