Getting JSONP to work
-
I’m calling the API like so:
$.ajax({ type: "GET", url: 'https://domain.com/wp-json/posts/<ID>/?_jsonp=?', dataType: 'jsonp', }).success( function(response){ console.log(JSON.stringify(response)); });
But keep getting an error message in Chrome:
Refused to execute script from XXX because its MIME type (‘application/json’) is not executable, and strict MIME type checking is enabled.
After testing, there were two solutions I could see. Removing the call to the “nosniff” header in JSON_API_Server::serve_request() or changing the content type if a jsonp request is detected like so:
if ( isset( $_GET['_jsonp'] ) && $jsonp_enabled ) { $this->send_header( 'Content-Type', 'application/javascript; charset=' . get_option( 'blog_charset' ), true ); } else { $this->send_header( 'Content-Type', 'application/json; charset=' . get_option( 'blog_charset' ), true ); }
But I’m not sure this is 100% accurate
Viewing 5 replies - 1 through 5 (of 5 total)
Viewing 5 replies - 1 through 5 (of 5 total)
- The topic ‘Getting JSONP to work’ is closed to new replies.