Home >php教程 >PHP源码 >jsonp+php 实现跨域加载

jsonp+php 实现跨域加载

PHP中文网
PHP中文网Original
2016-05-25 16:59:581061browse

php 端: demo.php

<?php
    $result =array(&#39;content&#39;=>$_GET[&#39;content&#39;]);
    function jsonp($result){
        return $_GET[&#39;callback&#39;] . &#39;(&#39; . urldecode(json_encode($result)) . &#39;)&#39;;
    }
    echo jsonp($result);
?>

客户端:demo.js

<script>
       $.ajax({
                           url      : &#39;http://127.0.0.1/demo.php&#39;,
                           data     :{&#39;content&#39;:&#39;this is a test&#39;},
                           dataType : &#39;jsonp&#39;,
                           jsonp    : &#39;callback&#39;,
                           success  : function(res) {
                                      console.log(res);       
                           }
       });
</script>
Statement:
The content of this article is voluntarily contributed by netizens, and the copyright belongs to the original author. This site does not assume corresponding legal responsibility. If you find any content suspected of plagiarism or infringement, please contact admin@php.cn