CodeIgniter 中的 JSONP

发布于 2024-07-17 01:51:02 字数 1078 浏览 8 评论 0原文

我在 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 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(4

铁憨憨 2024-07-24 01:51:02

您的网址中的&符号是错误的:

loggedin/&jsoncallback=?

应该是类似于“运行”的内容

loggedin/?jsoncallback=?

我得到的响应

jsonp123( ...

,这意味着它尝试调用 jsonp123 函数,并且此时从未使用代码的 adat 部分。

The ampersand in your url is wrong:

loggedin/&jsoncallback=?

It should be something along the lines of

loggedin/?jsoncallback=?

Running that i get the response of

jsonp123( ...

Which means it attempts to call the jsonp123 function, and the adat part of your code is never used at this point.

∞觅青森が 2024-07-24 01:51:02

默认情况下 CI 不允许查询字符串,那么您是否在 config.php 中启用了 enable_query_strings

$config['enable_query_strings'] = TRUE;

By default CI doesn't allow query strings, so did you enable enable_query_strings in your config.php?

$config['enable_query_strings'] = TRUE;
情绪 2024-07-24 01:51:02

我认为您面临的问题是因为浏览器限制不允许跨域请求。 我猜测您正在将您的应用程序托管在其他域/子域上,这就是导致此问题的原因。

您需要使用的是 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.

如若梦似彩虹 2024-07-24 01:51:02

另请确保您在 config.php 中将 $config['allow_get_array'] 设置为 TRUE。 使用

$this->input->get();

这将允许您在控制器中

Also make sure that you have $config['allow_get_array'] set to TRUE in config.php. This will allow you to use

$this->input->get();

in your controllers.

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文