There’s two parts to this, the javascript ajax call needs specific variables included:
$.ajax({
type: "GET",
url: SSL_Ajax.ajaxurl,
cache: false,
dataType: "jsonp",
crossDomain: true,
data: {
action : 'ajaxSSL',
ajaxSSLNonce : SSL_Ajax.ajaxSSLNonce,
input : $('input[name=title]').val()
},
success: function( data ) {
console.log( 'success' );
console.log( data );
},
complete: function( data ) {
console.log( 'complete' );
console.log( data );
},
error: function( data ) {
console.log( 'error' );
console.log( data );
}
});
The important variables here for jsonp are cache
,dataType
, and crossDomain
. Note: when your dataType is jsonp it creates an additional varible ‘callback’. Also, by it’s very nature, jsonp is type
GET.
The other important thing to note is in the wp_ajax_nopriv_{action}
and wp_ajax_{action}
actions you need to include the callback and access-control header:
header("content-type: text/javascript; charset=utf-8");
header("access-control-allow-origin: *");
echo htmlspecialchars($_GET['callback']) . '(' . $response . ')';
// IMPORTANT: don't forget to "exit"
exit;
I wrote a about using jsonp in WordPress if you want more details.