带有Sencha Touch的JSONP无法正常工作 [英] JSONP with Sencha Touch not working

查看:102
本文介绍了带有Sencha Touch的JSONP无法正常工作的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在按照Sencha Touch的本教程进行操作: http://www.vimeo.com/15672696 完美的教程,完美无缺.

I was following this tutorial of Sencha Touch: http://www.vimeo.com/15672696 Perfect tutorial, worked flawless.

但是当我尝试为自己的项目重用此代码时,我将无法工作. 这是我做过的事:

But when I tried to reuse this code for my own project, I doesn't work. Here is wat I did:

在我的Sencha touch应用程序中,我编写了以下功能:

In my Sencha touch application I wrote the following function:

showContacts = function()
{
    Ext.util.JSONP.request({
        url: 'http://www.hotcoffee.be/check-relations/index.php/json/contactpersonen',
        callbackKey: "callback",
        params: {
            unique: Math.random()
        },
        callback: function(data)
        {
            var contacts = data.results;
            nameOfPanel.update(contacts);
        }
    });
}

首先,我用PHP(codeigniter)编写了自己的JSON文件:

First, I wrote my own JSON file with PHP (codeigniter):

<?
    $row[] = array(
        "name" => $item->name,
        "first_name" => $item->first_name,
        "avatar"=>$item->avatar
    );

    // PASSING THE ARRAY $row TO A VIEW
    // ON THE VIEW I OUTPUT THE ARRAY

    $this->output->set_content_type('application/json')->set_output(json_encode($row));
?>

(结果:请参见showContacts函数的URL)

(Result: see URL of showContacts function)

这没有用,所以我认为使用PHP文件作为JSON时出了点问题,所以我创建了一个JSON文件:

This didn't work, so I assumed something was wrong with using a PHP file as JSON, so I created a JSON file:

(结果: http://www.hotcoffee.be/check-relations /json/friends.json )

现在看来这也行不通.我花了好几个小时在脑海中挣扎... 我还在Chrome的调试器中收到以下消息:

Now it seems that also this didn't work. I'm breaking my head over it a lot of houres... I also receive the following messages in Chrome's debugger:

Resource interpreted as Script but transferred with MIME type application/json. (contactpersonen:-1)
Uncaught SyntaxError: Unexpected token : (contactpersonen:1)
Resource interpreted as Image but transferred with MIME type text/html. (csi:-1)

另一件事是,我可以确保面板和tpl的编写正确,因为使用硬编码的testdata可以正常工作.问题仅在于加载JSON文件.

Another thing is that I can ensure that the panels and tpl's are written well, because with hardcoded testdata it does work. Only loading the JSON file is the problem.

有人知道如何处理吗? 非常感谢你!

Anyone knows how to handle it? Thank you very much!

推荐答案

我在这里回答了类似的问题:

I answered a similar question here: Sencha seems to not like Rails' json. It is specific to Rails but the concept still applies.

基本上詹姆斯·皮尔斯是正确的.您返回的内容需要包装在标记和回调函数中.这将在页面上插入代码并运行脚本,从而具有调用您提供的功能的作用.

Essentially James Pearce is correct. What you are returning needs to be wrapped in a tag and the callback function. This will insert the code on your page and run the script, which has the effect of calling the function you provide.

$response = "<script type='text/javascript'>";
$response .= $_GET['callback'] . "(" . json_encode($row) . ")";
$response .= "</script>";
$this->output->set_content_type('application/json')->set_output($response);

这篇关于带有Sencha Touch的JSONP无法正常工作的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆