【问题标题】:How to use google code jquery $.jsonp如何使用谷歌代码 jquery $.jsonp
【发布时间】:2011-12-26 16:56:54
【问题描述】:

我有几个关于使用这个的问题。

1) 我需要从谷歌代码下载jquery.jsonp-2.1.4.min.js,但我应该把它放在哪个文件夹中? (我正在使用 xampp)在 C:\xamppC:\xampp\htdocs

2) 我应该写什么,以便我可以使用$.jsonp 将一些字符串从网络传递到我的本地服务器?我应该在“数据”输入中更改什么以传递变量str

$.jsonp({
    "url": "http://localhost/server.php",
    "data": {
        "alt": "json-in-script"
    },
    "success": function(userProfile) {
        alert("ha"); // handle user profile here 
    },
    "error": function(d,msg) {
        alert("Could not find user ");
    }
});

3) 如何在php页面中获取变量“str”?

<?php echo str; ?>

【问题讨论】:

    标签: jquery jsonp google-code


    【解决方案1】:

    当前版本的 jQuery 内置了 JSONP 支持。不再需要单独的 JSONP 库。

    你可以这样使用它:

    $.ajax({
        'url': "/path/to/your/endpoint.php?callback=?",
        'dataType': 'jsonp',
        'data': ...
    });
    

    data 中的值作为 URL 参数传递;因此,如果您使用的是 PHP,它们将显示在 $_GET 中。请注意,结果必须格式化为 JSON,并包装在回调中指定的函数中。适当的代码可能类似于:

    print $_GET['callback'] . '(' . json_encode($result) . ');';
    

    【讨论】:

    • 然而,如果我使用 $.jsonp 我该怎么办,似乎 data:{name="a"} 将在 url 参数中包含名称....这意味着我可以使用 | |回显 $_GET['name']|| ?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2011-08-03
    • 1970-01-01
    • 2022-10-08
    • 1970-01-01
    相关资源
    最近更新 更多