2011-08-09 74 views
0

爲什麼這個PHP沒有獲取我的鏈接數組?用jQuery發送JSON AJAX

function check_links() { 

     $matches = $this->input->get('links'); 


     if($matches == true) { 
      echo json_encode('matches is true'); 
     } else { 
      echo json_encode('matches is false'); 
     } 


     //echo json_encode($matches); 

    } 

的JS

 var linksStr = $("#links").val(); 
     var matches = linksStr.match(/\bhttps?:\/\/[^\s]+/gi); 

     alert(matches.length); 

     for(var i = 0; i < matches.length; i++) { 
      alert(matches[i]); 
     } 

     var links = JSON.stringify(matches); 

     $.ajax({ 
     type: 'GET', 
     dataType: 'json', 
     cache: false, 
     data: links, 
     url: 'publishlinks/check_links', 
     success:      
      function(response) { 

       alert(response); 

      } 


     }) 
+1

什麼'的var_dump($ _ GET)'說明了什麼?或者使用JS調試器,加載了哪個url? – TJHeuvel

回答

2

我糊塗了一下什麼是試圖在這裏實現。
但JSON.stringify需要分配一個值,

var links = JSON.stringify(matches); 

像鏈接

var links = 'links='+JSON.stringify(matches); 
在功能

然後,$matches現在應該包含您的JSON編碼的鏈接。
所以,你可以使用,

function check_links() { 
    $matches = $this->input->get('links'); 
    ... 
    $matches = json_decode($matches); // do stuff 
    ....