2017-04-30 72 views
0

我有一個ajax函數應該發佈一些數據到另一個頁面,但它似乎並沒有因爲某些原因發佈。ajax沒有發佈到頁面

我不知道爲什麼這不起作用,我一直在過去的一個小時裏看它。

Ajax的功能

$(document).ready(function() 
{ 
    $('.rate').click(function(){ 
     var ratingId = $('#ratingID').val(); 
     var clickBtnValue = $(this).val(); 
     var ajaxurl = 'ProcessRating.php', 

     data = {'action': clickBtnValue, 'ratingID': ratingId}; 

     $.post(ajaxurl, data, function (response) 
     { 
      alert("action performed successfully"); 
     }); 
    }); 
}); 

在主頁

<?php 
session_start(); 
echo $_SESSION["messege"]; 
?> 
<html> 
<head> 
    <title>TODO supply a title</title> 
    <meta charset="UTF-8"> 
    <meta name="viewport" content="width=device-width, initial-scale=1.0"> 
    <script 
    src="https://ajax.googleapis.com/ajax/libs/jquery/3.2.0/jquery.min.js"> 
    </script> 
    <script> 
    //The Ajax function is here 
    </script> 
</head> 

<body> 
    <form> 
    <input type="hidden" class="rate" id="ratingID" name="ratingID" value = 
    "<?php echo $article->getId()?>" /> 
    <input type="submit" class="rate" name="1" value="1" /> 
    <input type="submit" class="rate" name="2" value="2" /> 
    <input type="submit" class="rate" name="3" value="3" /> 
    <input type="submit" class="rate" name="4" value="4" /> 
    <input type="submit" class="rate" name="5" value="5" /> 
    </form> 
</body> 

在頁面ProcessRating.php

<?php 
session_start(); 

$_SESSION["messege"] = "got This far"; 

if (isset($_POST['action'])) 
{ 
    $id = $_POST['ratingID']; 

    switch ($_POST['action']) { 
     case '1': 
      updateRating($db, $id, "oneStar"); 
      break; 
     case '2': 
      updateRating($db, $id, "twoStar"); 
      break; 
     case '3': 
      updateRating($db, $id, "threeStar"); 
      break; 
     case '4': 
      updateRating($db, $id, "fourStar"); 
      break; 
     case '5': 
      updateRating($db, $id, "fiveStar"); 
      break; 
    } 

    $rating = getAvgRating($db, $id); 
    updateArticleRating($db, $id, $rating); 
} 
+2

你看過控制檯以及使用php的錯誤報告嗎? –

+0

我沒有看到你連接到ProcessRating.php上的數據庫的任何地方? – RiggsFolly

+0

它甚至沒有,因爲它不會迴應會話消息,所以我可以看到我到了下一頁 –

回答

2

1.因爲所有按鈕類型都是submit而且它們都在<form></form>之內。所以您必須使用preventDefault()來防止標準表單發佈。

做象下面這樣: -

$(document).ready(function(){ 
    $('.rate').click(function(e){ 
     e.preventDefault(); 
     var ratingId = $('#ratingID').val(); 

     var clickBtnValue = $(this).val(); 

     var ajaxurl = 'ProcessRating.php', 

     data = {'action': clickBtnValue, 'ratingID': ratingId}; 

     $.post(ajaxurl, data, function (response){ 
      alert("action performed successfully"); 
     }); 
    }); 
}); 

2.確保ajaxurl是正確的有(檢查控制檯此,如果有錯誤的網址,你會看到,在控制檯forsure)

重要說明: -

檢查您的瀏覽器控制器是否有錯誤?如果是的話告訴我們。

+0

好問題是在我的一個功能需要,需要有一個../上去一個文件夾。我通過使用console.log發現了這個 –

0

正如評論中所建議的那樣,請檢查通過控制檯返回到腳本的內容。你可以這樣的方式使用console.log(response)現在

jQuery.ajax({ 
    type: "POST", 
    url: "http://url", 
    data: "action=foobar", 
    success: function(response){ 
     console.log(response); 
    } 
}); 

,您可以檢查控制檯返回響應。

+0

你必須使用提供的代碼。不發明新東西 – RiggsFolly

+1

*「正如在評論」* - 正確;所以這更多的是一個榮耀的評論。 –