【问题标题】:Ajax success data called twiceAjax 成功数据调用了两次
【发布时间】:2017-01-16 04:39:51
【问题描述】:

我不明白为什么我的数据被调用了两次。我试图替换附加,但它不起作用。我想是因为我的控制器。

这是我的 Ajax 调用:

jQuery(document).ready(function($) {
$('#referenceProduit').change(function(){
    // On recupere la valeur de l'attribut value pour afficher tel ou tel resultat
    var req=$('#referenceProduit').val();
    // Requête ajax, appel du fichier function.php
    $.ajax({
        type: "post",
        url: "index.php?uc=gererReclamation&action=saisirReclamation",
        data: "referenceProduit="+req,
        dataType : "html",
        //affichage de l'erreur en cas de problème
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
        },
        // Function s'il n'y a pas de probleme
        success:function(data){
            //On affiche la réponse du serveur
            $('.result').empty();
            $('.result').prepend(data);
        }
    });
});

HTML 代码:

<div class="form-group">
    <label for="referenceProduit" class="col-sm-1 control-label">Reference</label>
    <div class="col-sm-2">
        <select class="form-control" name="referenceProduit" id="referenceProduit">
            <option selected="selected" disabled="disabled">Choisir</option>
            <?php foreach($lesProduits as $unProduit){?>
            <option name="<?php echo $unProduit['id'];?>" value="<?php echo $unProduit['id'];?>"><?php echo $unProduit['reference']?></option>
            <?php } ?>
        </select>
    </div>
    <div class="result"></div>
</div>

控制器

<?php
    $action = $_REQUEST['action'];
    switch($action){
        case 'accueil':{
            include("vue/v_accueil.php");
            break;
        }
        case 'saisirReclamation':{
            $lesSites = $pdo->getLesSites();
            $lesProduits = $pdo->getLesProduits();
            $lesClients = $pdo->getLesClients();
            $lesNatures = $pdo-> getLesNatures();
            $lesActivites = $pdo->getLesActivites();
            if(isset($_REQUEST['referenceProduit'])){
                $leProduit = $pdo->getLeProduit();
                foreach ($leProduit as $key => $value) {
                    echo '<input type="text" name="'.$key.'" value="'.$value.'"/>';
                }
            }
            include_once("vue/v_saisirReclamation.php");
            break;
        }
    }
?>

【问题讨论】:

  • 你能展示你的 HTML 代码吗?
  • 检查。可能您的 html 中有多次 referenceProduit id
  • 在我看来只有一个referenceProduit
  • 您是否尝试使用其他浏览器>
  • 改变这一行 $('.result').empty();转为 $('.result').html(' ');

标签: javascript php jquery ajax


【解决方案1】:

让我们看看你的代码:

jQuery(document).ready(function($) {
$('#referenceProduit').change(function(){
    //on recupere la valeur de l'attribut value pour afficher tel ou tel resultat
    var req=$('#referenceProduit').val();
    //requête ajax, appel du fichier function.php
    $.ajax({
        type: "post",
        url: "index.php?uc=gererReclamation&action=saisirReclamation",
        data: "referenceProduit="+req,
        dataType : "html",
        //affichage de l'erreur en cas de problème
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
        },
        //function s'il n'y a pas de probleme
        success:function(data){
            //On affiche la réponse du serveur
            $('.result').empty();
            $('.result').prepend(data);
        }
    });
});

您的内容触发了change 事件。您需要确保您的回复不会触发change。这是一个使用标志的简单解决方案:

jQuery(document).ready(function($) {
var shouldChange = true;
$('#referenceProduit').change(function(){
    if (!shouldChange) {
        return;
    }
    //on recupere la valeur de l'attribut value pour afficher tel ou tel resultat
    var req=$('#referenceProduit').val();
    //requête ajax, appel du fichier function.php
    $.ajax({
        type: "post",
        url: "index.php?uc=gererReclamation&action=saisirReclamation",
        data: "referenceProduit="+req,
        dataType : "html",
        //affichage de l'erreur en cas de problème
        error: function(XMLHttpRequest, textStatus, errorThrown) {
            alert(XMLHttpRequest + '--' + textStatus + '--' + errorThrown);
        },
        //function s'il n'y a pas de probleme
        success:function(data){
            //On affiche la réponse du serveur
            shouldChange = false;
            $('.result').empty();
            $('.result').prepend(data);
            shouldChange = true;
        }
    });
});

【讨论】:

  • 然后我该怎么办?
  • @LucasFrugier,你测试过这个吗?
  • console.log($shouldChange) ?
  • @LucasFrugier,你有没有试过用我提供的代码替换你的代码示例,看看它是否运行了两次?
  • 再次运行两次是的
【解决方案2】:
success:function(data){
    // On affiche la réponse du serveur
    $('.result').empty();
    $('.result').prepend(data);
}

用下面的代码代替上面的代码

success:function(data){
    // On affiche la réponse du serveur
    $('.result').html(data);
}

【讨论】:

  • 将变量data改为response in success:function(data)
  • 将结果类更改为 result1
  • 再次相同..:(
  • 评论这一行 $('.result').html(data);
  • 好的。所以你的结果是两次给出该html。检查你得到的结果html。这就是问题
【解决方案3】:

改变这一行

$('.result').empty();

$('.result').html(' ');

【讨论】:

    猜你喜欢
    • 1970-01-01
    • 2014-11-22
    • 2018-02-24
    • 1970-01-01
    • 2019-01-12
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2013-03-25
    相关资源
    最近更新 更多