问题
When trying to perform an API call to Twitter, I noticed I must use "jsonp" to bypass the cross-domain problem. Using "json" dataType will throw a 400 "Control-Allow-Origin" error.
On the other hand, when I'm switching to jsonp I'm always receiving a 400 error with the following message:
jquery.min.js:4 GET https://api.twitter.com/1.1/search/tweets.json?callback=aloha&q=dog
Any ideas on what I'm doing wrong here and how to get it fixed?
<script type="text/javascript">
function aloha(result) {
debugger;
};
var twitter_call = $.getJSON({
type: 'GET',
contentType: 'application/javascript',
dataType: 'jsonp',
cache: true,
jsonpCallback: 'aloha',
url: 'https://api.twitter.com/1.1/search/tweets.json',
crossDomain: true,
headers: {
"Authorization": "Bearer MY_BEARER_TOKEN"
},
data: {
"q":"dog"
}
});
</script>
回答1:
This is officially unsupported by Twitter as mentioned by their team on this topic I raised on their development forum:
https://twittercommunity.com/t/error-401-problems-with-jsonp-token-with-twitter-rest-api-ajax/73618
Any workarounds found for this topic will be posted directly in that link.
来源:https://stackoverflow.com/questions/39338836/calling-twitters-api-with-jsonp-datatype-will-throw-a-400-error