CodeIgniter 中的 JSONP
我在 CodeIgniter 中使用 jQuery JSONP 方法 $.getJSON
时遇到问题。 从中抓取 JSON 的 URL 如下:
http://spinly.000space.com/index.php/admin/isloggedin
问题是我有一个 demo.html
文件,它运行 $.getJSON
方法,并抓取数据来自我上面指出的 URL。
demo.html:
<html>
<head>
<script src="http://www.spinly.000space.com/public/js/jquery.js"></script>
<script>
$(document).ready(function(){
var myurl = "http://spinly.000space.com/index.php/admin/isloggedin/&jsoncallback=?";
//myurl = "http://com.lnk.bz/jsonp.php?sleep=3&jsoncallback=?";
$.getJSON(myurl,function(adat) {
alert(adat);
// clearTimeout(t);
});
});
</script>
</head>
<body>
<div id="images">
</div>
</body>
</html>
当我运行demo.html
时,什么也没有发生。 正如您所看到的,当我将 URL 更改为另一个不使用 CodeIgniter 作为框架的 URL 时,它应该会提醒返回的数据。 我运行了警报功能,但在这种情况下,当使用 CodeIgniter 备份的 URL 时,它不起作用。 有人能解决我的问题吗? 如果您给我一些反馈,我将非常感激。 提前致谢!
I have a problem with using the jQuery JSONP method $.getJSON
in CodeIgniter. The URL from which the JSON is grabbed from is the following:
http://spinly.000space.com/index.php/admin/isloggedin
The Problem is that I have a demo.html
file that runs the $.getJSON
method, and grabs the data from the URL I denoted above.
demo.html:
<html>
<head>
<script src="http://www.spinly.000space.com/public/js/jquery.js"></script>
<script>
$(document).ready(function(){
var myurl = "http://spinly.000space.com/index.php/admin/isloggedin/&jsoncallback=?";
//myurl = "http://com.lnk.bz/jsonp.php?sleep=3&jsoncallback=?";
$.getJSON(myurl,function(adat) {
alert(adat);
// clearTimeout(t);
});
});
</script>
</head>
<body>
<div id="images">
</div>
</body>
</html>
When I run demo.html
nothing happens. As you can see, it's supposed to alert the data returned when I change the URL to a another one that doesn't use CodeIgniter as the framework. I get alert function running, but in this case, while using the URL that's backed up with CodeIgniter, it doesn't work. Does anyone have a solution to my problem? I'd really appreciate if you gave me some feedback. Thanks in advance!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(4)
您的网址中的&符号是错误的:
应该是类似于“运行”的内容
我得到的响应
,这意味着它尝试调用 jsonp123 函数,并且此时从未使用代码的 adat 部分。
The ampersand in your url is wrong:
It should be something along the lines of
Running that i get the response of
Which means it attempts to call the jsonp123 function, and the adat part of your code is never used at this point.
默认情况下 CI 不允许查询字符串,那么您是否在 config.php 中启用了 enable_query_strings ?
By default CI doesn't allow query strings, so did you enable enable_query_strings in your config.php?
我认为您面临的问题是因为浏览器限制不允许跨域请求。 我猜测您正在将您的应用程序托管在其他域/子域上,这就是导致此问题的原因。
您需要使用的是 JSONP,即带填充的 Json。 详细信息在这里: http://en.wikipedia.org/wiki/JSONP
查看 jquerys 中的 jsonp ajax api 在这里: http://api.jquery.com/jQuery.ajax/ 并查看如果这解决了你的问题。
i think the issue that you are facing is because of the restriction by browsers on not allowing cross domain requests. I am guessing that you are hosting your app on some other domain/sub-domain and that is what is causing this issue.
What you need to use is JSONP i.e Json with padding. Details here: http://en.wikipedia.org/wiki/JSONP
Look at jsonp in jquerys ajax api here: http://api.jquery.com/jQuery.ajax/ and see if that solves your problem.
另请确保您在 config.php 中将
$config['allow_get_array']
设置为TRUE
。 使用$this->input->get();
。这将允许您在控制器中
Also make sure that you have
$config['allow_get_array']
set toTRUE
in config.php. This will allow you to use$this->input->get();
in your controllers.