使用基本的json获取twitter api

我是相当新的jQuery,我试图使用JSON获取twitter api tho我设法用php做了我写了这段简单的代码,但它似乎不起作用

(function() { $(document).ready(function() { $.getJSON("https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true",function(data) { var ragzor = data.name; $(".ragzor").text(ragzor); console.log(ragzor); }); return false; }); }); 

 jQuery(function($) { // Shorter for $(document).ready(function() {, and you make sure that $ refers to jQuery. $.ajax({ // All jQuery ajax calls go throw here. $.getJSON is just a rewrite function for $.ajax url: "https://api.twitter.com/1/users/show.json?screen_name=TwitterAPI&include_entities=true", dataType: "jsonp", // dataType set to jsonp (important) success: function( resp ) { console.log( resp ); // Here resp represents the data reseved from the ajax call. } }); }); 

jQuery源代码:

 $.getJSON = function( url, data, callback ) { return jQuery.get( url, data, callback, "json" ); } 

将您重定向到$.get

 jQuery.each( [ "get", "post" ], function( i, method ) { jQuery[ method ] = function( url, data, callback, type ) { // shift arguments if data argument was omitted if ( jQuery.isFunction( data ) ) { type = type || callback; callback = data; data = undefined; } return jQuery.ajax({ type: method, url: url, data: data, success: callback, dataType: type }); }; }); 

屁你可以看到这返回$.ajax的初始化版本


所以你的电话改写为:

 $.ajax({ url: "...", dataType: "json" // <- Note json not jsonp, success: function() { // ... } });