Support » Plugin: WP REST API (WP API) » Getting JSONP to work

  • Resolved darrinb

    (@dbmartin)


    I’m calling the API like so:

    $.ajax({
        type: "GET",
        url: 'http://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

    https://wordpress.org/plugins/json-rest-api/

Viewing 5 replies - 1 through 5 (of 5 total)
  • Looking at the code, this should be accurate (I’d opt for sending the alternative header over removing the type checking, however).

    Plugin Author Ryan McCue

    (@rmccue)

    Sorry about this! This is fixed in the current development version and will be included with version 1.2.

    bigfatfrog

    (@bigfatfrog)

    I’m writing an app using the Ionic framework and I’m getting the same problem.
    I’ve got the angular.js call:

    $http.jsonp("http://localhost/matImms/wp-json/posts?callback=JSON_CALLBACK").success(function (posts) {
                    this.postsData = posts;
                })

    The problem seems to be _jsonp is not being set at all – so the fix doesn’t work. However, even hacking the code so that ‘application/javascript’ is always return doesn’t seem to work – and I’m not sure how to fix this. Any ideas?

    lrz_463

    (@lrz_463)

    Same for me like @bigfatfrog!!

    bigfatfrog

    (@bigfatfrog)

    I used the $http.get method as a workround – and it seems to be working fine!

Viewing 5 replies - 1 through 5 (of 5 total)
  • The topic ‘Getting JSONP to work’ is closed to new replies.