簡體   English   中英

jquery ajax如何trat回調

[英]jquery ajax how to trat callback

我正在嘗試使用jQuery和ajax函數,以便不重新加載頁面。

所以我有一個基本的表格,我發送數據到這里的PHP文件我沒有問題。

我遇到的麻煩就是對待回報,因為即使查詢失敗,它也會一直成功。

在php文件中我有

<?php

//Instantiation de la connexion
require_once('../../config/connexion.php');
//Initialisation des fichiers requis
include_once('../../lib_php/librairie.php');


//Initialisation des variables
function RecordObjectif(){
    //On cherche l'id de l'utilisateur
    $id_student = fetchUserId();
    if(isset($_POST['objectif'])){
        $objectif = mysql_real_escape_string($_POST['objectif']);
        $query = "INSERT INTO ...";
        mysql_query($query);
        if(mysql_affected_rows()>=0){
            $out['erreur'] = 1;
        }else{
            $out['erreur'] = 0;
        }
        echo $out;
    }else{
        return null;
    }
}

echo RecordObjectif();

在jquery我的意思是Html頁面我有這個

 $.ajax({
                    type: 'POST',
                    url: 'pages_ajax/mes-objectifs.php',
                    data: { objectif: objectif,  nom_programme: nom_programme, date_depart : date_depart, date_fin: date_fin, statut:statut},
                    dataType: "json",
                    beforeSend:function(){
                        // this is where we append a loading image
                        $('#resultat').empty()
                        $('#resultat').attr('style','text-align:center');
                        $('#resultat').append('<img src="images/loading.gif" alt="Loading..." />');
                    },
                     success:function(data){
                    // successful request; do something with the data
                    if(data.erreur==1){
                        $('#resultat').empty().removeAttr('style');
                        $('#resultat').attr('class','success').empty().append('<b>Enregistrement de votre programme terminé.</b>')
                    }
                },
                error:function(){
                    if(data.erreur==0){
                        // failed request; give feedback to user
                        $('#resultat').empty().removeAttr('style');
                        $('#resultat').attr('class','error').empty().append('<b>Erreur lors de l\'enregistrement, veuillez recommencer dans quelques minutes.</b>');
                        }
}

我不知道如何對待回歸,它總是回歸我的成功。

if $out['erreur'] = 0我想顯示錯誤我不明白的是我總是成功回歸。

結果沒有解釋,我仍然有加載圖標。

任何形式的幫助將不勝感激。

您無法使用選擇器“。”在ajax請求的回調中訪問PHP變量。 但您可以像這樣訪問腳本回顯的數據:

.success(function(data) {
    if(data.response == 1) {
        alert('okay');
    }
});

接下來,如果您返回http錯誤代碼(除了200 OK),將調用錯誤回調。 只要您的腳本在沒有服務器端錯誤的情況下運行,您總是返回“200 OK”。 因此,您定義的錯誤函數將永遠不會被調用。 我的解決方案如下:

$.ajax({
    type: 'POST',
    url: 'pages_ajax/mes-objectifs.php',
    data: { objectif: objectif,  nom_programme: nom_programme, date_depart : date_depart, date_fin: date_fin, statut:statut},
    dataType: "json",
    beforeSend:function(){
        // this is where we append a loading image
        $('#resultat').empty()
        $('#resultat').attr('style','text-align:center');
        $('#resultat').append('<img src="images/loading.gif" alt="Loading..." />');
   },
   always:function(data){
         $('#resultat').empty().removeAttr('style');

        if(data.response==1){
               // successful request; do something with the data
               $('#resultat').attr('class','success').empty().append('<b>Enregistrement de votre programme terminé.</b>')
        } else {
               // failed request; give feedback to user
               $('#resultat').attr('class','error').empty().append('<b>Erreur lors de l\'enregistrement, veuillez recommencer dans quelques minutes.</b>');
        }
   }
   });

並改變你的PHP腳本的行

echo $out;

echo $out['erreur'];

你也可以在數組上做一個json_encode()並返回php數組的json數據,但在我看來這是一個開銷。

好的,你的代碼中有很多東西你需要看一下:

  • 你正在回應RecordObjectif函數的結果。 這很好,但是在函數本身你應該return值而不是回顯它。
  • 當ajax請求自己成功時調用$.ajax成功函數,並且不依賴於你的php函數正在輸出什么
  • 要訪問從php返回的數據,你有jsonencode的php輸出,並再次在javascript中使用JSON.parse

只是為這個問題提供一些工作代碼對你沒有幫助,因為據我所知,你對ajax請求的工作方式缺乏基本的了解。 我的建議是搜索ajax請求的一些簡單示例,並嘗試真正了解代碼的哪一部分正在做什么。

error:function(){刪除下面的代碼塊error:function(){

if(data.erreur==0){
    // failed request; give feedback to user
    $('#resultat').empty().removeAttr('style');
    $('#resultat').attr('class','error').empty().append('<b>Erreur lors de l\'enregistrement, veuillez recommencer dans quelques minutes.</b>');
}

並將此success:function(data){如下所示:

success:function(data){
    // successful request; do something with the data
    if(data.erreur==1){
        $('#resultat').empty().removeAttr('style');
        $('#resultat').attr('class','success').empty().append('<b>Enregistrement de votre programme terminé.</b>')
    }
    // if faliure
    else if(data.erreur==0){
        // failed request; give feedback to user
        $('#resultat').empty().removeAttr('style');
        $('#resultat').attr('class','error').empty().append('<b>Erreur lors de l\'enregistrement, veuillez recommencer dans quelques minutes.</b>');
    }
}

這應該做的工作!

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM