2013-09-25 54 views
1

我使用YiiBackboneBoilerplate。我想用数据填充模型是这样的:骨干取()发送text/html的请求类型,而不是JSON

--model--

define([ 
    'jquery', 
    'underscore', 
    'backbone' 
    ], function($, _, Backbone) { 

     var EvaluateModel = Backbone.Model.extend({ 

      urlRoot: 'evaluate/process', 
      defaults: { 
       title: '', 
       state: 1 
      } 
     }); 

     return EvaluateModel; 
    }); 

--in我view--

initialize:function() { 
     var result = new Evaluate({id:this.id}); 
     result.fetch({ 
      success: function(result, response) { 
       JSON.stringify(result.model); 
      } 
     }); 
    }, 

- Yii的行动 -

public function actionProcess() { 
    //I have tryed this 
    echo json_encode('test'); 
    Yii::app()->end(); 

    //and this 
    $this->sendResponse(200, CJSON::encode(array('title' => 'test'))); 
} 

我得到的textStatus:parsererror和从服务器返回的结果包含当前页面的html

此外,fetch()应该根据app.js中的初始设置发送POST请求类型,但类型是 GET

--app.js--

// initialize Http object to make backbone work with POST instead of GET 
Http.initialize({type:'POST'}); 

出了什么问题?

回答

0

我没有看到任何文件指的Http.initialize。那是从哪里来的?既然你取出由服务器的现有模式,骨干会发出默认GET请求,模拟HTTP(http://backbonejs.org/#Sync-emulateHTTP)时也是如此。其次,可能发生的一件事是您的服务器端框架忽略了application/json请求的内容类型类型,并且只会返回JSON,您可以调用mypage.json URL(即它只考虑扩展名)。

默认情况下,反向键将获得(例如)/api/cars/12与内容类型application/json得到既然在内容类型请求JSON相关的汽车模型实例qith编号12的数据,预计JSON归还。一些Web框架只将默认有自己的API返回JSON数据如果改为调用(例如)/api/cars/12.json。或者,您的API可能无法返回JSON(即,API不会自动执行此操作,而您自己也没有配置它)。

+0

>我没有看到任何文件指的Http.initialize是http.js这是在应用程序文件夹\程序\ JS \库\ utils的\ http.js为YiiBackboneBoilerplate http://www.yiiframework.com/wiki/390/yiibackboneboilerplate-project-setup-for-your-yii-backbonejs-applications/ – user2814599

+0

>,只会返回JSON是你打电话mypage.json网址(即它只考虑扩展名)。请详细解释一下 – user2814599

+0

我澄清了JSON调用问题。但是我仍然无法找到解释'Http.initialize'应该做什么的文档:在你链接的页面的任何地方都没有提到它。 –

0

我从服务器发送数据杰森这样的:

protected function sendResponse($status = 200, $body = '', $contentType = 'application/json') 
{ 
    // Set the status 
    $statusHeader = 'HTTP/1.1 ' . $status . ' ' . $this->getStatusCodeMessage($status); 
    header($statusHeader); 
    // Set the content type 
    header('Content-type: ' . $contentType); 

    echo $body; 
    Yii::app()->end(); 
} 


protected function getStatusCodeMessage($status) 
    { 
     $codes = array(
      100 => 'Continue', 
      101 => 'Switching Protocols', 
      200 => 'OK', 
      201 => 'Created', 
      202 => 'Accepted', 
      203 => 'Non-Authoritative Information', 
      204 => 'No Content', 
      205 => 'Reset Content', 
      206 => 'Partial Content', 
      300 => 'Multiple Choices', 
      301 => 'Moved Permanently', 
      302 => 'Found', 
      303 => 'See Other', 
      304 => 'Not Modified', 
      305 => 'Use Proxy', 
      306 => '(Unused)', 
      307 => 'Temporary Redirect', 
      400 => 'Bad Request', 
      401 => 'Unauthorized', 
      402 => 'Payment Required', 
      403 => 'Forbidden', 
      404 => 'Not Found', 
      405 => 'Method Not Allowed', 
      406 => 'Not Acceptable', 
      407 => 'Proxy Authentication Required', 
      408 => 'Request Timeout', 
      409 => 'Conflict', 
      410 => 'Gone', 
      411 => 'Length Required', 
      412 => 'Precondition Failed', 
      413 => 'Request Entity Too Large', 
      414 => 'Request-URI Too Long', 
      415 => 'Unsupported Media Type', 
      416 => 'Requested Range Not Satisfiable', 
      417 => 'Expectation Failed', 
      500 => 'Internal Server Error', 
      501 => 'Not Implemented', 
      502 => 'Bad Gateway', 
      503 => 'Service Unavailable', 
      504 => 'Gateway Timeout', 
      505 => 'HTTP Version Not Supported', 
     ); 
     return (isset($codes[$status])) ? $codes[$status] : ''; 
    }