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() {
// ...
}
});