2013-03-06 197 views
0

我使用這個簡單PHP框架中的一些冷門與他人Custom PHPAjax請求返回的HTML,而不是實際值 - PHP

後,我有以下控制器:

<?php 

class TestController extends BaseController 
{ 

    public function __construct($action, $urlValues) { 
     parent::__construct($action, $urlValues); 
    } 

    public function deploy_test() { 
     echo json_encode("helloworld"); 
    } 
} 
?> 

這功能由.js功能激活稱爲test()

function test() 
{ 

    var data = { 
     config : $("#config").val(), 
     machines : $("#machines").val(), 
     test  : $("#test").val(), 
     product : $("#product").val(), 
    }; 

    $.post('./index.php/test/deploy_test/', data, 
    function(answer){ 
     create_bar(); 
     console.log(answer); 
    }); 
} 

不過,我回來是index.php頁面,html本身,而不是"helloworld"字符串按預期方式。我不知道發生了什麼事。願有人請我嗎? 輸出是:

<!DOCTYPE html> 
<html> 
<head> 
<title>Deployer</title> 
</head> 
<body> 
<?xml version="1.0" encoding="UTF-8"?> 
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd"> 
<html xmlns="http://www.w3.org/1999/xhtml" xml:lang="en" lang="en"> 
+0

發佈你正在獲得的回報或截圖 – 2013-03-06 20:22:37

+0

@DavidNguyen補充! – cybertextron 2013-03-06 20:23:55

+0

框架能夠圍繞/而不是你的輸出噴出樣板數據嗎?我知道CakePHP,默認情況下會輸出大量的CSS等。 – 2013-03-06 20:26:20

回答

0

您將需要發送json_encode("helloworld")所以之前設置在服務器端正確的頭信息ajax可以在閱讀答案之前預測json值。例如第一線可能是:

header('Cache-Control: no-cache, must-revalidate'); // no cache 
header('Expires: Mon, 26 Jul 1997 05:00:00 GMT'); 
header('Content-type: application/json'); // json type 

你還需要,像大衛建議,在你的Ajax請求添加dataType: json

相關問題