Refused to execute script on jsonp request

This question already has an answer here:

  • What is JSONP all about? [duplicate] 7 answers

  • In PHP, set before the echo

    header('Content-Type: application/json');
    

    And your PHP doesn't have any callback (you're not returning JSONP). You want it to look something like..

    echo $_GET['callback'].'('.json_encode($arr).')';
    

    Edited

    I missed the absolute obvious. JSONP is for cross site AJAX calls. You're using it as a script.

    This is a very awkward way to load data. The normal way is to load the data through an AJAX call within a script, not load variables globally.

    If you really want to go with your design, keep the application/json tag but go back to your original echo json_encode($arr); You can also drop the ?callback='callback' -- it does nothing.


    you need to force specify output type. Try to add

    header('Content-Type: application/json');
    

    before output the array in test.php

    and then you need to wrap entire encoded output to a callback name so js on a client-side can work with it:

    $callbackName = filter_input(INPUT_GET, 'callback');
    echo $callbackName . '(' . json_encode($arr) . ')';
    
    链接地址: http://www.djcxy.com/p/8350.html

    上一篇: JSONP内部工作与AJAX

    下一篇: 拒绝在jsonp请求上执行脚本