0

我正在尝试使用从 codeiniter restful api 生成的(JSON)数据来提供 sencha。我做了基于 Net.tuts API CI TUTORIAL的 RESTful API

我收到错误 Uncaught SyntaxError: Unexpected token : Here is my Sencha code and CI Code

CI代码

function user_get()
    {
      if(!$this->get('id'))  
      {  
            $this->response(NULL, 400);  
      }  

        $user = $this->user_model->get( $this->get('id') );  

        if($user)  
        {  
            $this->response($user, 200); // 200 being the HTTP response code  
        }  

        else  
        {  
            $this->response(NULL, 404);  
        }     
    }

生成的 JSON

{
name: "ALEX JOHN",
country: "USA",
city: "NY"
}

煎茶代码

Ext.application({
    name: 'Sencha',

    launch: function() {

    Ext.create('Ext.DataView', {

    fullscreen: true,
    store: {
        autoLoad: true,
        fields: ['name', 'country','city'],

        proxy: {
            type: 'jsonp',
            url: 'http://localhost/rest_api/index.php/users/user/id/2',
            callbackKey: 'callback',
            callback: function(result) {
              console.log(result)  ;
            },
            reader: {
                type: 'json'
            }
        }
    },

    itemConfig: {
        tpl: '<p>{city}</p>'
    }
});
    }
});

我无法弄清楚该区域来自哪里。它来自我的 JSON 数据格式吗? sencha 是如何使用它的?请有人帮助。谢谢

4

1 回答 1

0

如果您请求 JSONP,则需要将返回的 JSON 包装在回调中。在您的控制器中,尝试将您的有效响应替换为:

//Is the GET field callback set?  If so, wrap the user object in it
if (isset($_GET['callback'])){
  $user=$_GET['callback'].'('.json_encode($user).')';
  //And manually create the response 
  header("Status: 200 OK"."\r\n");
  header("Date: ".gmdate(DATE_RFC822)."\r\n");
  header("Content-Type: application/javascript charset=UTF-8\r\n");
  header("Content-Length: " . strval(strlen($user))."\r\n");
  echo $user;
} else  //Use thhe API to gnerate the response as normal
$this->response($user, 200);
于 2012-08-13T22:05:34.133 回答