2012-04-23 55 views
0

这是我的js代码。无法仅接收来自JQuery的JSON ajax调用

.... 
var arrayData = {"projectId": projectId, "stateId":stateId}; 
$.ajax({ 
       type: "GET", 
       contentType: "application/json; charset=utf-8", 
       url: "/bidboldly/projects/editproject/",     
       data: arrayData, 
       success : function(response) {        
        alert(response);    
       }, 
       error: function(){ 
        alert("error"); 
       } 
      }) 

服务器端。

$city = $this->City->find('list',array('conditions' =>array('City.status'=>1, 'City.state_id'=>$this->params['url']['stateId']),'fields'=>array('City.id','City.city'),'order'=>array('City.city ASC'))); 
      echo json_encode($city); 

之后,在客户端我收到了城市列表+ html。像这样的东西。

..."Young America" [20292]=> string(10) "Zanesville" } <html><head>...... 

为什么要传输HTML?

+0

您的回音后添加退出:) – jantimon 2012-04-23 22:37:43

+0

你还在剧本中做什么?您是否使用将模板中的任何输出放入模板的框架? ''年轻美国人'[20292] =>字符串(10)“Zanesville”}'看起来并不像JSON(更像'var_dump')。 – 2012-04-23 22:40:14

+0

@Ghommey你在开玩笑吗? :) – emilan 2012-04-23 22:40:23

回答

1

本文http://book.cakephp.org/1.1/view/316/Helpers说,你应该使用AJAX渲染布局:

$this->layout = "ajax"; 

另一个整版Ajax和蛋糕PHP有用的文章是这个: http://ahsanity.wordpress.com/2007/02/23/get-started-with-ajax-in-cakephp/

+0

这就是为什么我喜欢写我自己的fw的原因,当你知道应该怎么做时,调试更容易。 +1挖掘那个:p – 2012-04-23 22:52:32

+0

@Sampo:那么如果你已经和任何框架阅读文档,那么你一般知道最新情况。由于您创建了该文件并且知道它并不像拥有清晰的IMO文档那样好 - 因为您是唯一知道并且很可能不会总是帮助维护项目的人。还有,当我主张蛋糕的时候,我会采取这样的态度......我不是一个粉丝......全部:-) – prodigitalson 2012-04-23 22:56:33