2013-01-02 63 views
0

早上,CodeIgniter和阿賈克斯問題

我一直在過去4天我的大腦和眼睛,我看不出爲什麼這不工作。基本上它是一個簡單的密碼更改腳本,運行完美無需Ajax。但是當我用ajax對它進行分層時,它似乎不能識別它,我看不出爲什麼。也許你們中的一位偉大的技術人員可以向我展示道路。

只是爲了確認。它似乎沒有組織,這是一個Ajax請求,並只保留每次我按下提交鍵

所以表格

<form action="<?php echo site_url('site/new_password'); ?>" method="post" class="box validate" id="change_password_form"> 

    <div class="header"> 
     <h2>Change Password</h2> 
    </div> 

    <div class="content"> 

     <?php if (isset($no_match)) {?> 
     <div class="alert error closeEverywhere"> 
      <span class="icon"></span> 
      <strong>Error !</strong>&nbsp;&nbsp;Passwords don't match! 
     </div> 
     <?php } ?> 
     <?php if (isset($changed)) {?> 
     <div class="alert success closeEverywhere"> 
      <span class="icon"></span> 
      <strong>Success !</strong>&nbsp;&nbsp;Password was successfully changed 
     </div> 
     <?php } ?> 
     <div class="alert error closeEverywhere" id="alertMessage"> 
      <span class="icon"></span> 
      <strong>Error !</strong>&nbsp;&nbsp;Password don't match! 
     </div> 
     <div class="alert success closeEverywhere" id="successMessage"> 
      <span class="icon"></span> 
      <strong>Success !</strong>&nbsp;&nbsp;Password was successfully changed 
     </div> 
     <!-- The form --> 
     <div class="form-box"> 

      <div class="row"> 
       <label for="change_pw"> 
        <strong>Password</strong> 
        <small></small> 
       </label> 
       <div> 
        <input tabindex=1 type="password" class="required noerror" name="change_pw" id="change_pw" /> 
        <?php echo form_error('change_pw','<label class="error" for="change_pw" generated="true">','</label>'); ?> 
       </div> 
      </div> 

      <div class="row"> 
       <label for="change_pw_conf"> 
        <strong>Again</strong> 
        <small>Password Confirmation</small> 
       </label> 
       <div> 
        <input tabindex=2 type="password" class="required noerror" name="change_pw_conf" id="change_pw_conf" /> 
        <?php echo form_error('change_pw_conf','<label class="error" for="change_pw_conf" generated="true">','</label>'); ?> 
       </div> 
      </div> 

     </div><!-- End of .form-box --> 

    </div><!-- End of .content --> 

    <div class="actions"> 
     <div class="left"> 
     </div> 
     <div class="right"> 
      <input tabindex=3 type="submit" value="Change Password" name="change_btn" id="change_btn" /> 
     </div> 
    </div><!-- End of .actions --> 

</form> 

而且AJAX段時間重新加載頁面

$(document).ready(function() { 
$('#login_form').on('submit',function() { 

    $.post(base_url+'site/login',$('#login_form').serialize(),function(data) { 
     if(!data || data.status !=1) 
     { 
      showError(); 
      return false; 
     } 
     setTimeout("window.location.href='"+base_url+"site/new_password'", 1000); 

    },'json'); 
    return false; 
}); 

$('#change_password_form').on('submit',function() { 

     $.post(base_url+'site/new_password',$('#change_password_form').serialize(),function(data) { 
     if(!data || data.status !=1) 
     { 
      showError(); 
      return false; 
     } 
     alert('Success'); 
     showSuccess(); 
     setTimeout("window.location.href='"+base_url+"member_section'", 1000); 

    },'json'); 
    return false; 
}); 

function showError() { 
    $('#alertMessage').slideDown(500); 
} 

function showSuccess() { 
    $('#successMessage').slideDown(500); 
} 
}); 

最後控制器方法

function new_password() 
    { 
     if ($this->input->is_ajax_request()) 
     { 
      $return_arr['status'] = 0; 
      echo json_encode($return_arr); // return value 
      exit(); 
     } 

     $this->load->view('site/new_password_view'); 
    } 
+0

具有u看到XHR請求數據和響應數據? U可以使用chorme和FF與螢火蟲做 – casper123

回答

1

我是否錯過了你在JavaScript中定義base_url?如果不是,我認爲你的問題不是你的問題,你的代碼不會被髮送到合法的URL。 base_url是一個CodeIgniter PHP函數。因此,例如下面的行

$.post(base_url+'site/new_password',$('#change_password_form').serialize(),function(data) { 

應改爲:

$.post("<?=base_url();?>site/new_password",$('#change_password_form').serialize(),function(data) { 
+0

Base_Url沒有被定義,它只是被誤解了。非常感謝你對遲交回復的幫助和道歉 –