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

jsonp+php 实现跨域加载

PHP中文网
Release: 2016-05-25 16:59:58
Original
1037 people have browsed it

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);
?>
Copy after login

客户端: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>
Copy after login
Related labels:
source:php.cn
Statement of this Website
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
Latest Articles by Author
Popular Recommendations
Popular Tutorials
More>
Latest Downloads
More>
Web Effects
Website Source Code
Website Materials
Front End Template