无法从JQuery ajax调用中仅接收JSON

时间:2012-04-23 22:35:37

标签: php javascript jquery ajax json

这是我的js代码。

....
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?

1 个答案:

答案 0 :(得分:1)

这篇文章http://book.cakephp.org/1.1/view/316/Helpers说你应该使用ajax渲染布局:

$this->layout = "ajax";

关于ajax和cake php的另一页有用的文章就是这个: http://ahsanity.wordpress.com/2007/02/23/get-started-with-ajax-in-cakephp/