2013-04-08 77 views
0

這裏是我的link_handler.php作爲post方法操作。PHP-jquery在使用類時不會得到帖子返回

require_once 'download_handler.php'; 

if ($_POST) { 

    if (empty($_POST['link'])) { 
     $mahar['success'] = false; 
     $mahar['error'] = "you're not insert any link"; 
     echo json_encode($mahar); //work, show error string on #success 
     exit(); 
    } 

    $download = new Hijack($_POST['link']); 
    $download->check_link(); 
    $download->execute(); 
    $mahar['success'] = $download->result['success']; // produce success(bool) = false 
    $mahar['error'] = $download->result['error']; // produce error string : there's some error on parsing some stuff 
    echo json_encode($mahar); //not work show nothing on #success 
} 

,這是我的jQuery語法

$(document).ready(function() { 
    $("#go").click(function() { 
    $("#success").fadeOut(); 
     $.post('link_handler.php', {link: $("#url").val()}, 
     function(mahar) { 
      if (mahar.success) { 
       $("#success").html(mahar.link); 
      } else { 
       $("#success").html(mahar.error); 
      } 
     }, 'json'); 
     return false; 
    }) 
}); 

任何人都可以在這裏解釋發生了什麼事?

即時學習阿賈克斯在這裏。

+0

在第二個'json_encode()'之後沒有'exit()'。 – 2013-04-08 04:34:15

+0

打開Chrome,按F12然後按網絡按鈕,查看XHR請求(底部有按鈕),然後發出請求。看看你有什麼responde ..你有返回數據還是你有404? – Svetoslav 2013-04-08 04:38:24

+0

@Jack它不是必需的。腳本在那裏結束。 – 2013-04-08 04:39:27

回答

0

最有可能的情況是,輸出不是(只)JSON。

考慮以下語句:

$download = new Hijack($_POST['link']); 
$download->check_link(); 
$download->execute(); 
$mahar['success'] = $download->result['success']; 
$mahar['error'] = $download->result['error']; 

所有這些聲明可能會產生警告或通知,這會搞亂了JSON語法。您可以通過檢查瀏覽器的網絡選項卡來查看此內容並查找響應內容。

您應該解決您的代碼可能產生的所有警告;您也可以查看logging the warnings

+0

yohoo ..你是對的.. 我的班上有一些警告。 這都是因爲「」東西 感謝隊友。 – 2013-04-08 06:01:01

0

嘗試那些2個東西..

1日。爲了您的$ _ POST鏈接設置完整路徑沒有你的域名,就好像它是

http://mydomain.com/link_handler.php - 把 '/link_handler.php'

http://mydomain.com/something/link_handler.php - 把 '/something/link_handler.php'

2nd。在你link_handler.php在第一行認沽:

exit(json_encode(array('success'=>"Your request is successfull"))); 

如果收到responde在瀏覽器上,你的問題是你的PHP代碼中..

第三名。在你的jquery把1警報('我們正在發佈'); $ .post之前,以確保您的javascripts工作..

+0

好吧,如果我不在輸入文本中插入任何鏈接,if(空)語句工作正常,它會在#success上顯示錯誤。 但如果我在輸入文本上插入鏈接(是的,我讓它返回錯誤)沒有其他消息#success – 2013-04-08 04:55:51

+1

測試你的PHP代碼然後..你的功能是否有效?並在第二個json_encode之後放置出口。 – Svetoslav 2013-04-08 04:58:10

+0

是的,它是在劫持類:) 謝謝隊友。 – 2013-04-08 06:01:36

相關問題