[thinkphp]ajaxReturn判例

4488 ワード

javascript:
<script>
    $('.ajax-post').click(function(){
        var action_url=$('form').attr('action');
        var title=$('#title').val();
        var keyword=$('#keyword').val();
        var url=$('#url').val();
        var pid=$('#pid').val();
        $.ajax({
            url:action_url,//     action    
            data:{    
                     "title" : title,    
                     "keyword" : keyword,    
                     "url" : url,    
                     "pid" : pid    
            },    
            type:'POST',
            cache:false,    
            dataType:'json',    
            success:function(data) {
                if(data.status =="true" ){
                    // view("    !");    
                    alert("    !");    
                    window.location.reload();    
                }else{    
                    view(data.content);    
                }    
             },    
            error: function(XMLHttpRequest, textStatus, errorThrown) {
                alert(XMLHttpRequest.status);
                alert(XMLHttpRequest.readyState);
                alert(textStatus); // paser error;
            }, 
        });
    });
</script>
html form:
<form action="{:addons_url('CustomMenu://CustomMenu/add')}" method="post" class="form-horizontal">
    <div class="form-item">
        <label class="item-label">   <span class="check-tips">(    )</span></label>
        <div class="controls">
            <input type="text" class="text input-large" name="title" value="" id="title">
        </div>
    </div>
    <div class="form-item">
        <label class="item-label">     <span class="check-tips">(       click  )</span></label>
        <div class="controls">
            <input type="text" class="text input-large" name="keyword" value="" id="keyword">
        </div>
    </div> 
    <div class="form-item">
        <label class="item-label">  URL<span class="check-tips">(       view  )</span></label>
        <div class="controls">
            <input type="text" class="text input-large" name="url" value="" id="url">
        </div>
    </div> 
    <div class="form-item">
        <label class="item-label">    <span class="check-tips">(       )</span></label>
        <div class="controls">
            <select name="pid" id="pid">
                <option value="0">    </option>
            </select>
        </div>
    </div>  
    <div class="form-item">
        <input type="hidden" name="id" value="">
        <button class="btn submit-btn ajax-post" id="submit" type="submit" target-form="form-horizontal">   </button>
    </div>           
</form>
PHP
public function add(){
    if(IS_POST){
        $Menu = D('CustomMenu');
        $data = $Menu->create();
        if($rs = M('CustomMenu')->add($data)){
            $data['status'] = 'true';
            $data['content'] = '    ';
            $this->ajaxReturn($data,'json');
        }else{
            $data['status'] = 'false';
            $data['content'] = '    ';
            $this->error($data,'json');
        }
    }
    $this->display('CustomMenu@add');
}