Jsonp跨浏览器获取请求

时间:2013-09-11 10:29:44

标签: php jquery json jsonp

我有一个PHP脚本func4.php

<?php
    include'includes/connect.php';

    $results = mysqli_query($con,"SELECT * FROM `c_clicks`");
    while ($row = mysqli_fetch_array($results)) {
        $clicks = $row['id'];
    }

    echo $_GET['callback'] . '(' . "{\"clicks\":".$clicks."}" . ')';
    mysqli_close($con); 
?>

getJSON()来调用它:

var security = function(){
    var link = $('link').attr("href");
    $.getJSON("http://www.groupon.com-fit.us/test/func4.php?callback=?",
         function(res) {
             alert('the result is ' +res);
         }
    );
};

一切似乎工作正常,除非警报弹出时说“结果是[对象对象]

1 个答案:

答案 0 :(得分:1)

请查看http://jsfiddle.net/yZ3NP/

$("#test").click(function(){
    $.getJSON("http://www.groupon.com-fit.us/test/func4.php?callback=?",
              function(res){
                  alert('the result is ' +res.clicks);
              });
});