programing

Ajax 성공 및 오류 함수 오류

jooyons 2023. 3. 16. 21:20
반응형

Ajax 성공 및 오류 함수 오류

jQuery ajax가 제대로 작동하지 않습니다.데이터베이스를 업데이트하기 위해 PHP 페이지로 이동하지만 성공 또는 오류 옵션을 위해 스크립트로 돌아가지 않습니다.

코드는 다음과 같습니다.

$(document).ready(function(){  
        $("form#updatejob").submit(function() {  
            function textreplace(x) {return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");}
            // we want to store the values from the form input box, then send via ajax below
            var job     = $("#job").attr("value");
            var description     = $("#description").val();
            description.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
            var startDate   = $("#startDate").attr("value");
            var releaseDate = $("#releaseDate").attr("value");  
            var status  = $("#status").attr("value"); 
            $.ajax({
                beforeSend:textreplace(description),
                type: "POST",  
                url: "updatedjob.php",
                data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
                success: function(){  
                    $("form#updatejob").hide(function(){$("div.success").fadeIn();});  
                },
                error: function(XMLHttpRequest, textStatus, errorThrown) { 
                    alert("Status: " + textStatus); alert("Error: " + errorThrown); 
                }       
            });
            return false;  
        });  
});

그리고 PHP:

<?php 
    include("connect.php"); 
    $job = trim($_POST['job']); 
    $startDate = trim($_POST['startDate']); 
    $releaseDate = trim($_POST['releaseDate']); 
    $mysqlstartdate = date('Y-m-d', strtotime($startDate)); 
    $mysqlreleasedate = date('Y-m-d', strtotime($releaseDate)); 
    $description = trim($_POST['description']); 
    $status = trim($_POST['status']); 
    $update = "UPDATE jobs SET startDate = '$mysqlstartdate', releaseDate = '$mysqlreleasedate', description = '$description', status = '$status' WHERE jobID = '$job' "; 
    $rsUpdate = mysql_query($update);
// or die(mysql_error()); mysql_close(); 
?>

이것을 시험해 보세요.

$.ajax({
    beforeSend: function() { textreplace(description); },
    type: "POST",  
    url: "updatedjob.php",
    data: "jobID="+ job +"& description="+ description +"& startDate="+ startDate +"& releaseDate="+ releaseDate +"& status="+ status, 
    success: function(){  
        $("form#updatejob").hide(function(){$("div.success").fadeIn();});  
    },
    error: function(XMLHttpRequest, textStatus, errorThrown) { 
        alert("Status: " + textStatus); alert("Error: " + errorThrown); 
    }       
});

beforeSend속성이 로 설정됩니다.function() { textreplace(description); }대신textreplace(description).그beforeSend속성에는 함수가 필요합니다.

에러를 검출하려면 , 다음의 방법을 사용할 수도 있습니다.

$.ajax({
    url: url, 
    success: function (data) {
        // Handle success here
        $('#editor-content-container').html(data);
        $('#editor-container').modal('show');
    },
    cache: false
}).fail(function (jqXHR, textStatus, error) {
    // Handle error here
    $('#editor-content-container').html(jqXHR.responseText);
    $('#editor-container').modal('show');
});

같은 문제가 발생하여 dataType = "text" 행을 ajax 콜에 추가하여 해결했습니다.서버에서 반환될 것으로 예상되는 응답과 dataType을 일치시킵니다("insert successful" 또는 "something wronged" 오류 메시지).

에러 고유의 로직은, 다음과 같이 실장할 수 있습니다.

error: function (XMLHttpRequest, textStatus, errorThrown) {
    if (textStatus == 'Unauthorized') {
        alert('custom message. Error: ' + errorThrown);
    } else {
        alert('custom message. Error: ' + errorThrown);
    }
}

이것은 오래된 투고일 수도 있지만, 저는 php에서 반환되는 것이 없고, 당신의 성공 함수는 다음과 같은 입력이 없다는 것을 깨달았습니다.success:function(e){}도움이 됐으면 좋겠네요

이것으로 모든 문제가 해결되는 것은 아니지만 함수(텍스트) 내에서 사용하고 있는 변수가 (x)에서 전달하고 있는 파라미터와 동일하지 않습니다.

변경:

function textreplace(x) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

수신인:

function textreplace(text) {
    return text.replace(/[-[\]{}()*+?.,\\^$|#\s]/g, "\\$&");
}

뭔가 도움이 될 것 같아요

get용으로 구현된 데이터가 포함된 게시 유형을 보냅니다.폼은 다음과 같아야 합니다.

$.ajax({
url: url,
method: "POST",
data: {data1:"data1",data2:"data2"},
...
 $.ajax({
         type:'POST',
         url: 'ajaxRequest.php',
         data:{
         userEmail : userEmail
         },
         success:function(data){
         if(data == "error"){
                $('#ShowError').show().text("Email dosen't Match ");
                $('#ShowSuccess').hide();
               }
               else{
                 $('#ShowSuccess').show().text(data);
                }
              }
            });

언급URL : https://stackoverflow.com/questions/8918248/ajax-success-and-error-function-failure

반응형