Simple jQuery, PHP and JSONP example?

后端 未结 7 2284
佛祖请我去吃肉
佛祖请我去吃肉 2020-11-22 16:36

I am facing the same-origin policy problem, and by researching the subject, I found that the best way for my particular project would be to use JSONP to do cross-origin requ

7条回答
  •  感动是毒
    2020-11-22 16:52

    To make the server respond with a valid JSONP array, wrap the JSON in brackets () and preprend the callback:

    echo $_GET['callback']."([{'fullname' : 'Jeff Hansen'}])";
    

    Using json_encode() will convert a native PHP array into JSON:

    $array = array(
        'fullname' => 'Jeff Hansen',
        'address' => 'somewhere no.3'
    );
    echo $_GET['callback']."(".json_encode($array).")";
    

提交回复
热议问题