角 3 2 ajax

kohana 3 2 with ajax

我正在尝试学习 kohana,但在使用 kohana+ajax 时遇到了问题,控制台告诉我关于整页+ print_r + name\phone 的 ajax 响应的响应,如果 运行 没有 kohana 的例子,只是 php+ajax ,一切正常

更新:

等待:姓名:$姓名,phone:$phone号码。

recieve(look console.log): 来自主模板的页面 + print_r+ ajax 和 name:undefined 的响应, phone:未定义。

这里是重现行为的代码:

Controller/example.php:

 <?php defined('SYSPATH') or die('No direct script access.');    
    class Controller_Example extends Controller_Common
    {
        public function action_index()
        {    
            $content = View::factory('/example/show')
                ->bind('example', $example)
                ->bind('errors', $errors);    
            if ($this->request->is_ajax()) {     
                if (isset($_POST["name"]) && isset($_POST["phonenumber"]) ) {    
                       $result = array(
                        'name' => $_POST["name"],
                        'phonenumber' => $_POST["phonenumber"]
                    );
                              print_r($result);                    
                            echo json_encode($result);                    
                }  
            }
            else        $this->template->content=$content;    
        }}

查看:

<!doctype html>
<html lang="en">
<head>
        <script type="text/javascript" src="https://ajax.googleapis.com/ajax/libs/jquery/2.0.3/jquery.min.js"></script>
</head>
<body>
<form method="post" id="ajax_form" action="" >
    <input type="text" name="name" placeholder="NAME" /><br>
    <input type="text" name="phonenumber" placeholder="YOUR PHONE" /><br>
    <input type="button" id="btn" value="send" />
</form>
<br>
<div id="result_form"></div>
</body>
</html>
<script>
$( document ).ready(function() {
        $("#btn").click(
            function(){
                sendAjaxForm('result_form', 'ajax_form');
                return false;
            }
        );
    });    
    function sendAjaxForm(result_form, ajax_form) {
        jQuery.ajax({
            url: '', 
            type:     "POST", 
            dataType: "html",
            data: jQuery("#"+ajax_form).serialize(),  
                success: function(response) { 
                    //console.log(response);
                   //result = jQuery.parseJSON(response);                   
                    result = response;                   
                    console.log(result);
                    document.getElementById(result_form).innerHTML = "name: "+result.name+"<br>phone: "+result.phonenumber;

            },
            error: function(response) { 
                document.getElementById(result_form).innerHTML = "Error";
            }
        });
    }
</script>

Controller/Common:

<?php defined('SYSPATH') or die('No direct script access.');
abstract class Controller_Common extends Controller_Template {
    public $template = 'main';
    public function before()
    {
        parent::before();
        View::set_global('title', 'My Site');
        View::set_global('description', 'My Site');
        $this->template->content = '';
        $this->template->styles = array('main');
        $this->template->scripts = '';
    }
} // End Common

主模板视图:

<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title><?php echo $title; ?></title>
    <meta name="description" content="<?php echo $description; ?>" />
    <?php foreach($styles as $style): ?>
        <link href="<?php echo URL::base(); ?>public/css/<?php echo $style; ?>.css"
              rel="stylesheet" type="text/css" />
    <?php endforeach; ?>
</head>
<body>
<div class="layer">
    <div class="container">
        <div class="header"><h1>Logo</h1></div>
        <div class="left">
            <h3>Menu</h3>
            <br />
            <ul>
                <li><a href="<?php echo URL::site(); ?>">Home</a></li>
                <li><a href="<?php echo URL::site('about'); ?>">about</a></li>
                <li><a href="<?php echo URL::site('reg'); ?>">Reg</a></li>
                <li><a href="<?php echo URL::site('contacts'); ?>">contacts</a></li>
                <li><a href="<?php echo URL::site('articles'); ?>">Articles</a></li>
            </ul>
            <li><a href="<?php echo URL::site('articles/article'); ?>">Article1</a></li>
                                <li><a href="<?php echo URL::site('articles/article1'); ?>">Article2</a></li></ul>
        </div>
        <div class="content"><?php echo $content; ?></div>
        <div class="clearing"></div>
        <div class="footer">2011 All rights reserved</div>
    </div>
</div>
</body>
</html>

Console.log :

Array
(
    [name] => 1
    [phonenumber] => 1
)
{"name":"1","phonenumber":"1"}<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN"
        "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
    <meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
    <title>My site</title>
    <meta name="description" content="My site" />
            <link href="/public/css/main.css"
              rel="stylesheet" type="text/css" />    
</head>
<body>
<div class="layer">
    <div class="container">
        <div class="header"><h1>Logo</h1></div>
        <div class="left">
            <h3>Menu</h3>
            <br />
            <ul>
                <li><a href="/">Home</a></li>
                <li><a href="/about">about</a></li>
                <li><a href="/reg">Reg</a></li>
                <li><a href="/contacts">contacts</a></li>
                <li><a href="/articles">articles</a></li>
            </ul>
            <li><a href="/articles/article">article1</a></li>
                                <li><a href="/articles/article1">article2</a></li></ul>
        </div>
        <div class="content"></div>
        <div class="clearing"></div>
        <div class="footer">2011 All rights reserved</div>
    </div>
</div>
</body>
</html>

问题已解决,请从以下地址获取答案:

作者: croll , Source
json_encode(); 之后添加 exit; // 由于未知原因,主模板已附加到 ajax 的答案并 exit; 修复它。

and result = response;=>result = jQuery.parseJSON(response); // 用于解码 ajax 答案