简体   繁体   English

jQuery ajax可在Chrome中使用,但不能在Firefox或IE中使用

[英]jQuery ajax works in Chrome but not Firefox or IE

Fair warning: I am no expert, but I did manage to get this far. 合理的警告:我不是专家,但是我确实做到了这一点。 My code isn't beautiful and it is rough. 我的代码不好看,很粗糙。 It is a fairly complex system so don't be shy to ask questions. 这是一个相当复杂的系统,因此不要害羞地提出问题。

So I have an annoying problem where my code works in chrome but nowhere else. 所以我有一个烦人的问题,我的代码在chrome中工作,但在其他地方没有。 It seems like none of my javascript is working in either Firefox or IE. 看来我的JavaScript都无法在Firefox或IE中运行。 PLEASE NOTE THAT EVERY TIME YOU SEE PHP INSIDE A DIV IT SIMPLY REPRESENTS THE # OF THE POST INSIDE THE DATABASE. 请注意,每次您在分区中看到PHP时,它都会简单地表示数据库中的POST数量。

My code displays posts where each post is paired with a like and dislike button built with spans. 我的代码显示了帖子,其中每个帖子都与使用跨度构建的“喜欢”和“不喜欢”按钮配对。 There is a checkbox that shows/hides all liked posts and another that does the same for disliked posts when selected. 有一个复选框显示/隐藏所有喜欢的帖子,而另一个复选框则在选中后对不喜欢的帖子执行相同的操作。 When a user likes or dislikes a post by clicking the button, values are sent to my DB through ajax (to check.php) so they can be recalled on future visits. 当用户通过单击按钮喜欢或不喜欢某个帖子时,值将通过ajax(到check.php)发送到我的数据库,以便将来访问时可以将其调出。

Again, it all works fine in Chrome but not in IE and Firefox. 同样,它们在Chrome中都可以正常运行,但在IE和Firefox中却不能。

Also, unless I insert the values into my userPosts table in my database manually first, no new posts and values are saved in my DB. 另外,除非我先将值手动插入数据库的userPosts表中,否则不会在数据库中保存任何新的帖子和值。 For example, if my DB already has values for posts 1-3, all future decisions by the user to like/dislike those posts are sent and saved no problem but if I add a new post (post4) and the user likes or dislikes it, no values get sent... it seems like the INSERT doesn't work in check.php whereas the UPDATE functions just fine. 例如,如果我的数据库已经具有第1-3个帖子的值,那么用户以后喜欢或不喜欢这些帖子的所有将来决定都将发送并保存,但没有问题,但是如果我添加了一个新帖子(第4个帖子),并且用户喜欢或不喜欢它,不会发送任何值...似乎INSERT在check.php中不起作用,而UPDATE函数就可以了。

Here is the jQuery that sits inside the loop, you should find it annotated to your satisfaction: 这是循环中的jQuery,您应该找到满意的注释:

<script type="text/javascript">
$(document).ready(function() {

// Declare variables
    var checked = <?php echo $row['value']; ?>; //get value of Liked or Disliked from database
    var postID = <?php echo $row['postID']; ?>; //get post ID from database
    var userID = <?php echo $current_user->ID; ?>; //get the wordpress user's ID
    var showLikes = $("input[name*='show_likes']"); //represents checkbox for Hide Liked
    var showDislikes = $("input[name*='show_dislikes']"); //represents checkbox for Hide Disliked

// Set the remembered Liked and Disliked buttons
    if (checked == 1) {
        $('#post_<?php echo $row['postID']; ?>').addClass('like'); 
        $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
    } else if (checked == 0) {
        $('#post_<?php echo $row['postID']; ?>').addClass('dislike'); 
        $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
    }

//When Liked button is clicked do this
$('#like_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '1';

// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });

// If post is Disliked, change to Liked
    $('#post_<?php echo $row['postID']; ?>').removeClass('dislike').addClass('like');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgon').addClass('dislikeimgoff');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgoff').addClass('likeimgon');
// If Hide Liked checkbox is on, toggle the post
    if (showLikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }

    return false;
});

//When Disliked button is clicked do this
$('#dislike_<?php echo $row['postID']; ?>').click(function() {
// Declare variables
    var value = '0';
// Send values to database
    $.ajax({
        url: 'check.php',
        type: 'POST',
        data: 'userID=' + userID + '&postID=' + postID + '&value=' + value,
        success: function(result) {
            $('#Message_<?php echo $row['postID']; ?>').html('').html(result).prependTo('#post_<?php echo $row['postID']; ?>');
        }
    });

// If post is Liked, change to Disliked
    $('#post_<?php echo $row['postID']; ?>').removeClass('like').addClass('dislike');
    $('#like_<?php echo $row['postID']; ?>').removeClass('likeimgon').addClass('likeimgoff');
    $('#dislike_<?php echo $row['postID']; ?>').removeClass('dislikeimgoff').addClass('dislikeimgon');
// If Hide Disliked checkbox is on, toggle the post
    if (showDislikes.attr('checked')) {
        $('#post_<?php echo $row['postID']; ?>').toggle();
    }

    return false;
});

//When Hide Liked checkbox clicked, toggle all Liked posts.
    $("input[name*='show_likes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.like')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });

//When Hide Disliked checkbox clicked, toggle all Disliked posts.
    $("input[name*='show_dislikes']").click(function() {
        if ($('#post_<?php echo $row['postID']; ?>').is('.dislike')) {
            $('#post_<?php echo $row['postID']; ?>').toggle();
        }
    });

});
</script>

Here is the code for each post, also sitting in the loop followed by the #Message that appears when the ajax returns the output of check.php and finally closes the loop: 这是每个帖子的代码,它们也位于循环中,然后是#Message,当ajax返回check.php的输出并最终关闭循环时,该消息将出现:

<div id="post_<?php echo $row['postID']; ?>" class="post">
<div id="post_<?php echo $row['postID']; ?>_inside" class="inside">
    <div id="like">
        <a id="like_<?php echo $row['postID']; ?>" class="likeimgoff" href="#"><span></span></a>
    </div>
    <div id="dislike">
        <a id="dislike_<?php echo $row['postID']; ?>" class="dislikeimgoff" href="#"><span></span></a>
    </div>
    <b><?php echo $row['Title']; ?></b><br>
    <?php echo $row['Description']; ?><br>
</div>
</div>
<div id="Message_<?php echo $row['postID']; ?>" class="reminder"></div>

<?php 
} 
?>

</div>

Here is check.php: 这是check.php:

<?php
mysql_connect("name.database.com", "username", "password") or die(mysql_error());
mysql_select_db("databasename") or die(mysql_error());

if (isset($_POST['userID'])){
$userID = mysql_real_escape_string($_POST['userID']);   
}else{
echo "No userID";
}

if (isset($_POST['postID'])){
$postID = mysql_real_escape_string($_POST['postID']);   
}else{
echo "No postID";
}

if (isset($_POST['value'])){
$value = mysql_real_escape_string($_POST['value']);   
}else{
echo "No value";
}

$query = mysql_query("SELECT * FROM userPosts WHERE userID='$userID' AND postID='$postID';") or die(mysql_error()); 

if (mysql_num_rows($query) > 0) { 
mysql_query("UPDATE userPosts SET value='$value' WHERE userID='$userID' AND postID='$postID';") or die(mysql_error());
} else {
mysql_query("INSERT INTO userPosts (userID, postID, value) VALUES ('$userID', '$postID', '$value') ") or die(mysql_error()); 
} 

echo "UserID: " .$userID. " PostID: " .$postID. " Value: " .$value;
?>

So there you have it. 所以你有它。 I know it is a lot of code, so please don't shy away and feel free to ask questions! 我知道这是很多代码,所以请不要回避,随时提出问题!

Sorry, I didn't took the time to read your whole post but usually this type of problems are of a javascript error. 抱歉,我没有花时间阅读您的全文,但通常这种类型的问题是javascript错误。 If you don't have it, install Firebug on Firefox and see in the firebug console if you have any errors, also look if the ajax call is made and what answer you get. 如果没有,请在Firefox上安装Firebug,并在firebug控制台中查看是否有任何错误,还要查看是否进行了ajax调用以及得到的答案。

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 jQuery Ajax-重新加载Image onClick可与Chrome一起使用,但不适用于Firefox和IE - JQuery Ajax - reload Image onClick works with chrome but not with firefox nor with IE 此代码可以在ie 10上正常运行,但不能在chrome和firefox上运行(javascript / ajax部分) - this code works fine on ie 10 but not on chrome and firefox (javascript/ajax portion) Javascript / Ajax可在Mozilla firefox中使用,但不能在Google Chrome和IE中使用 - Javascript / Ajax works in Mozilla firefox but not in Google Chrome and IE Ajax post适用于chrome,但使用ie和firefox的服务器上未接收到任何数据 - Ajax post works in chrome but no data recieved on server with ie and firefox 为什么Ajax代码可在IE,Chrome,FireFox5.0中运行,而在FireFox 3中却无法运行? - Why the ajax code works in IE, chrome, FireFox5.0 but not works in FireFox 3? 查询适用于Firefox,但不适用于ie和chrome - query works on firefox but not on ie and chrome 表格适用于Chrome和IE,但不适用于Firefox - Form works in Chrome and IE but not firefox 我有用于通过ajax或jquery更新mysql的表,并且适用于firefox,opera,chrome和safari,但不适用于IE8 - I have table that i use to update mysql via ajax or jquery and works for firefox,opera,chrome and safari but not IE8 使用jQuery AJAX传递变量在Firefox和Safari中不起作用,在Chrome中有效 - Passing variable with jQuery AJAX not working in Firefox and Safari, works in Chrome jQuery .ajax()GET请求无法从Firefox运行。 与Chrome搭配使用 - jQuery .ajax() GET request not working from firefox. Works with Chrome
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM