【发布时间】:2015-11-24 23:00:04
【问题描述】:
这个 ajax 请求将两个参数 x 和 y 发送到 php 控制器:
$.ajax({
url : 'feuilles/test',
data: {
x: x, y: y
},
type : 'post',
dataType : 'json',
success : function(res){
$.each(res, function(index, element) {
alert("index "+index + " element "+element);
});
},
error : function(result, statut, erreur){
console.log(result);
},
complete : function(resultat, statut){
//"completed"
}
});
这是 php 控制器:
public function test(){
$x = $this->request->data['x'];
$y = $this->request->data['y'];
$actualFeuille = $this->request->session()->read('actualFeuille');
$res = $actualFeuille->getListOfPossibilies(0,2);
$this->set('res', $res);
}
$res 得到一个数组,这是它的内容:
[
(int) 0 => (int) 1,
(int) 1 => (int) 2,
(int) 2 => (int) 3,
(int) 3 => (int) 4,
(int) 4 => (int) 5,
(int) 5 => (int) 6,
(int) 6 => (int) 7,
(int) 7 => (int) 8,
(int) 8 => (int) 9,
(int) 9 => (int) 10,
(int) 10 => (int) 11,
(int) 11 => (int) 12,
(int) 12 => (int) 13,
(int) 13 => (int) 14
]
当我返回响应时,它显示存在错误,事实是响应 html 内容中收到的正文就像以下示例一样:
"<div class="cake-debug-output"> <span><strong>\src\Model\Entity\Feuille.php</strong> (line <strong>68</strong>)</span> <pre class="cake-debug"> (int) 15 </pre> </div><div class="cake-debug-output"> <span><strong>\src\Model\Entity\Feuille.php</strong> (line <strong>61</strong>)</span> <pre class="cake-debug"> [ (int) 0 => (int) 1, (int) 1 => (int) 2, (int) 2 => (int) 3, (int) 3 => (int) 4, (int) 4 => (int) 5, (int) 5 => (int) 6, (int) 6 => (int) 7, (int) 7 => (int) 8, (int) 8 => (int) 9, (int) 9 => (int) 10, (int) 10 => (int) 11, (int) 11 => (int) 12, (int) 12 => (int) 13, (int) 13 => (int) 14 ] </pre> </div>{ "res": [ 1, 2, 3, 4, 5, 6, 7, 8, 9, 10, 11, 12, 13, 14 ] }"
我们可以看到 body 中包含了数组,但是由于 body 中包含的 html,它会显示错误。 我的问题是如何仅在响应正文中获取数组并删除所有无用的 html。
【问题讨论】:
标签: php jquery ajax cakephp controller