2011-07-24 56 views
1

我一直在試圖弄清楚這一點,但它似乎比我第一次想到的更難。然而,我想要做的是做一個Ajax發佈請求,但POST發送時似乎是空的。jQuery與Codeigniter AJAX發佈問題

我的HTML文件

<div id="statusUpdate"> 
     <?php echo form_open(base_url() . 'profile/statusUpdate', array('id' => 'statusUpdateForm', 'name' => 'statusUpdateForm')); ?> 
      <input type="text" value="Hva tenker du på?" name="profileUpdate" id="profileUpdate" onfocus="if(this.value == 'Hva tenker du på?')this.value=''" onblur="if(this.value == '')this.value='Hva tenker du på?'" /> 
      <input type="submit" value="" name="profileUpdateButton" id="profileUpdateButton" /> 
     <?php echo form_close(); ?> 
    </div> 

我的JavaScript

 $('#statusUpdateForm').submit(function() { 

     $.ajax({ // Starter Ajax Call 

      method: "POST", 
      url: baseurl + 'profile/statusUpdate', 
      data: $('#statusUpdateForm').serialize(), 
      success: function(data) { 
       alert(data); 
      } 

     }); 

     return false; 

    }); 

我的PHP(我的一些在控制器medhod的)

// Check if the input is a ajax request 
    if($this->input->is_ajax_request()) { 
     echo $_POST['profileUpdate']; 
    } 

注意,當我在控制器中放置回聲「Hello World」等時,我會在javascript的警報框中獲得「Hello World」。

我也試過在$ _ POST一個的var_dump並返回陣列(0){}當我試圖輸出特定的$ _ POST [「profileUpdate」]變量我得到這個樣子,

錯誤

enter image description here

我也做從seralize功能我JS一個警告,這是我得到了什麼,

enter image description here

是否有任何人誰知道我可以解決THI問題?

+0

檢查HTTP頭,實際上是發送了什麼到你的腳本? –

+0

檢查的最佳方法是什麼? – Dexty

+0

您可以使用Firebug或其他插件查看它們,例如Live HTTP Headers –

回答

2

嘗試更改methodtype

我猜腳本正在執行GET請求,這是使用ajax()而不是POST請求時的默認設置。就像這樣:

$.ajax({ // Starter Ajax Call 

     // "method" isn't an option of $.ajax 
     // method: "POST", 
     type: "POST", 

     url: baseurl + 'profile/statusUpdate', 
     data: $('#statusUpdateForm').serialize(), 
     success: function(data) { 
      alert(data); 
     } 

    }); 
+0

您的意思是輸入:「GET」? – Dexty

+2

不,因爲那樣您將執行GET請求,但您想要執行POST請求。 我的意思是你使用了方法:「POST」,但這不是$ .ajax的選項。正確的選項是鍵入,因此鍵入:「POST」 –

+0

@韋斯利默奇:謝謝澄清我的文章:) –

0

嘗試下面的代碼

在視圖中添加下列表格

<?php echo form_open('welcome/CreateStudentsAjax'); ?> 

    <label for="roll">Student Roll Number</label> 
    <input type="text" id="txtRoll" value="" name="roll"/> 

    <label for="Name">Students Name</label> 
    <input type="text" id="txtName" value="" name="name"/> 

    <label for="Phone">Phone Number</label> 
    <input type="text" id="txtPhone" value="" name="phone"/> 

    <input type="submit" name="submit" value="Insert New Students" /> 

    <?php echo '</form>'; ?> 

jQuery的一部分是低於

  $(document).ready(function(){ 

       $('form').submit(function(){ 
        //alert('ok');  
        $.ajax({ 
         url:this.action, 
         **type:this.method,** 
         data:$(this).serialize(), 
         success:function(data){ 
          var obj = $.parseJSON(data); 

          if(obj['roll']!=null) 
          {        
           $('#message').text(""); 
           $('#message').html(obj['roll']); 
           $('#message').append(obj['name']); 
           $('#message').append(obj['phone']); 
          } 
          else 
          {        
           $('#message').text(""); 
           $('#message').html(obj); 
          } 

         }, 
         erro:function(){ 
          alert("Please Try Again"); 
         }       
        }); 
        return false; 
       });       
      }); 

     </script>