Sencha 2.0 和 Codeigniter RESTful API 生成未捕获的语法错误:意外令牌:

Sencha 2.0 and Codeigniter RESTful API generates Uncaught SyntaxError: Unexpected token :

本文关键字:语法 错误 令牌 意外 Codeigniter RESTful API Sencha      更新时间:2023-09-26

我正在尝试将 sencha 提供从 codeiniter restful API 生成的 (JSON) 数据。我做了基于Net.tuts CI API教程的RESTful API

我收到错误未捕获语法错误:意外令牌:这是我的煎茶代码和CI代码

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 数据格式吗?煎茶是如何消费的?好心有人帮忙。谢谢

如果您要请求 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);