The Ajax callback function behaves randomly. What have I done wrong?

I have an ajax function that behaves randomly. Sometimes a warning is displayed success finally. In other cases, the second warning is that Failure: my status is 500.

promptIdpassed from the calling function. I checked prompturland promptId. A valid value from is promptIddisplayed in both cases (success and failure). The audio file that I am trying to play also plays in both cases (success and failure). I cannot understand the reason for this random behavior of the displayed alerts.

If 500 errorsuitable, it means that the resource was not found, but my application has access to the resource (which plays the audio file).

function ajax_playPrompt(promptId) {
alert(promptId)


 var playPromptUrl = soapUrl + "?action=playPrompt&promptId=" + escape(promptId) +     "&parentSessionId=" + parentSessionId;
alert(playPromptUrl);
playPrompt_http_request = getNewHttpRequest('text/plain');
playPrompt_http_request.onreadystatechange = callback_ajax_playPrompt;  
playPrompt_http_request.open("GET", playPromptUrl, true);   
playPrompt_http_request.send(null);
 }

function callback_ajax_playPrompt() {
    if (playPrompt_http_request.readyState != 4) {
        alert("Returning bcause not 4");        
        return;
    }

    if (playPrompt_http_request.status == 200) {
        alert("Success finally");        
    } 
    else {        
        alert("Failure:My status is "+playPrompt_http_request.status );   // this gives status as 500     
    }
}

Also to support different browsers I use:

// to provide support for different browsers.
function getNewHttpRequest(contentType) {
    var myRequest;
    if (window.XMLHttpRequest) { // Mozilla, Safari, ...
        myRequest = new XMLHttpRequest();
    } else if (window.ActiveXObject) { // IE
        myRequest = new ActiveXObject("Microsoft.XMLHTTP");
    }
    if (myRequest.overrideMimeType) {
        myRequest.overrideMimeType(contentType);
    }
    return myRequest;
}

: . , ( ), .

+4
1

?

function ajax_playPrompt(promptId) {
       alert(promptId)
        var playPromptUrl = soapUrl + "?action=playPrompt&promptId=" + 
          escape(promptId) +     "&parentSessionId=" + parentSessionId;
       alert(playPromptUrl);
       var playPrompt_http_request = getNewHttpRequest('text/plain');
       playPrompt_http_request.onreadystatechange = function {
               if (playPrompt_http_request.readyState != 4) {
                      alert("Returning bcause not 4");        
                      return;
                  }

                     if (playPrompt_http_request.status == 200) {
                     alert("Success finally");        
              } else {        
                      alert("Failure:My status is "+playPrompt_http_request.status );
             }
       };  
       playPrompt_http_request.open("GET", playPromptUrl, true);   
       playPrompt_http_request.send(null);
 }
0

Source: https://habr.com/ru/post/1540635/


All Articles