码迷,mamicode.com
首页 > Web开发 > 详细

php 跨域

时间:2015-06-02 11:05:50      阅读:200      评论:0      收藏:0      [点我收藏+]

标签:

$(document).ready(function(){
 
    $("#btn").click(function(k) {
        //...
        var j = $("form").serializeArray();//序列化name/value
        $.ajax({
            type: ‘GET‘//这里用GET
            url: ‘ajax.php‘,
            dataType: ‘jsonp‘//类型
            data: j,
            jsonp: ‘callback‘, //jsonp回调参数,必需
            async: false,
            success: function(result) {//返回的json数据
                alert(result.message); //回调输出
                 
                result = result || {};
                if (result.msg==‘err‘){
                    alert(result.info);
                }else if (result.msg=="ok"){
                    alert(‘提交成功‘);
                }else{
                    alert(‘提交失败‘);
                }
                 
            },
            timeout: 3000
        })
        //...
    });
     
});
1
2
3
4
5
6
7
8
<?php
$callback = isset($_GET[‘callback‘]) ? trim($_GET[‘callback‘]) : ‘‘; //jsonp回调参数,必需
$date = array("age"=>$_GET[‘age‘], "message"=>$_GET[‘age‘]);
$date["msg"]="err";
$date["info"]="因人品问题,发送失败";
$tmp= json_encode($date); //json 数据
echo $callback . ‘(‘ . $tmp .‘)‘//返回格式,必需
?>

php 跨域

标签:

原文地址:http://www.cnblogs.com/huiting/p/4545615.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!