简体   繁体   English

Sencha 2.0和Codeigniter RESTful API生成未捕获的SyntaxError:意外令牌:

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

I am trying to feed sencha with (JSON) data generated from codeiniter restful api. 我正在尝试使用从codeiniter restful api生成的(JSON)数据来填充sencha。 I did the the RESTful API based on the Net.tuts CI API TUTORIAL 我基于Net.tuts CI API教程完成了RESTful API

I get the error Uncaught SyntaxError: Unexpected token : Here is my Sencha code and CI Code 我收到错误未捕获语法错误:意外令牌:这是我的Sencha代码和CI代码

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);  
        }     
    }

GENERATED JSON 生成的JSON

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

SENCHA CODE SENCHA代码

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>'
    }
});
    }
});

I cannot figure out where the area comes from. 我不知道该区域来自哪里。 Is it from my JSON Data format?How sencha is consuming it? 它来自我的JSON数据格式吗?sencha如何使用它? Kindly someone help. 请有人帮助。 Thank you 谢谢

If you're requesting JSONP, youll need to wrap your returned JSON in a callback. 如果您请求JSONP,则需要将返回的JSON包装在回调中。 In your controller, try replacing your valid response with: 在您的控制器中,尝试将您的有效响应替换为:

//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);

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM