zoukankan      html  css  js  c++  java
  • jsonp例子php

    html:

    <!doctype html>
    <html>
    <head>
    <meta charset="utf-8">
    <title>test</title>
    <script src="./jquery.min.js"></script>
    <script src="./ajax.js"></script>
    </head>

    <body>
    <form name="form">
    <input type="text" name="names">
    <input type="text" name="ages">
    <input type="button" id="btn" value="button" />
    </form>
    </body>
    </html>

    jquery文件自己准备

    ajax.js:

    $(document).ready(function(){

    $("#btn").click(function(k) {
    //...
    var j = $("form").serializeArray();//序列化name/value
    $.ajax({
    type: 'GET', //这里用GET
    url: 'http://woaiwojia.online/jsonp/index.php',
    dataType: 'jsonp', //类型
    data: j,
    jsonp: 'callback', //jsonp回调参数,必需
    jsonpCallback:"success_jsonpCallback",
    async: false,
    success: function(result) {//返回的json数据
    alert(result.names+'**'+result.ages); //回调输出



    },

    })
    //...
    });

    });

    index.php

    <?php
    $callback = isset($_GET['callback']) ? trim($_GET['callback']) : ''; //jsonp回调参数,必需
    $date = array("names"=>$_GET['names'], "ages"=>$_GET['ages']);
    $tmp= json_encode($date); //json 数据
    echo $callback . '(' . $tmp .')'; //返回格式,必需
    ?>

  • 相关阅读:
    three.js
    three.js
    three.js
    反射API提供的常用类和函数
    PHP控制反转(IOC)和依赖注入(DI)
    优化思路以及优化过程
    nginx的缓存设置提高性能
    网页内容的压缩编码与传输速度优化
    nginx日志按日期自动切割脚本
    mysql数据备份
  • 原文地址:https://www.cnblogs.com/luziluck/p/8136580.html
Copyright © 2011-2022 走看看