0

My ajax call no longer works if i remove async:false, Could you please suggest better approach without using asynch:false.

        var json = JSON.stringify(mark.toJSON());
        var src = "";
        if (json.length > 1024) {
            $.ajax(Constants.Url_Base + 'Annotations/CreateMessage', {
                data: { jsAnnotation: json },
                type: 'post',
                async: false,//added so that events executes in proper order
                success: function (resp) {
                    if (resp.status !== 'ok') {
                        ErrorHandler.addErrors(resp.message, css.warningErrorClass, css.warningErrorClassTag, css.inputErrorClass, '');
                    }
                    else {
                        var messageId = encodeURIComponent(resp.result);
                        json = messageId;
                        
                    }
                }
            });
        }
      
            src = Constants.Url_Base + "Annotations/GetAnnotationPng?jsAnnotation=" + encodeURIComponent(json);
            var pdto = this.getPDTO();
            var dpi = Math.max(pdto.get('RezX'), pdto.get('RezY'));
            if (dpi > 0) {
                src += "&dpi=" + dpi;   
        }
        return src;  
    }```
ankit
  • 1
  • In what way does it 'no longer work'? Presumably this routine fires multiple times and you need to send the data in sequence to the server. If so, then you would want to create a message queue, and have a function that runs the first item in the queue and loops after it has been delivered. – David Oct 16 '20 at 06:55

1 Answers1

0

Probably this, The reason its not working because it return src early, So .done() will happened after ajax.

var json = JSON.stringify(mark.toJSON());
    var src = "";
    if (json.length > 1024) {
        $.ajax(Constants.Url_Base + 'Annotations/CreateMessage', {
            data: { jsAnnotation: json },
            type: 'post',
            async: false,//added so that events executes in proper order
            success: function (resp) {
                if (resp.status !== 'ok') {
                    ErrorHandler.addErrors(resp.message, css.warningErrorClass, css.warningErrorClassTag, css.inputErrorClass, '');
                }
                else {
                    var messageId = encodeURIComponent(resp.result);
                    json = messageId;
                    
                }
            }
        }).done(function(){

        src = Constants.Url_Base + "Annotations/GetAnnotationPng?jsAnnotation=" + encodeURIComponent(json);
        var pdto = this.getPDTO();
        var dpi = Math.max(pdto.get('RezX'), pdto.get('RezY'));
        if (dpi > 0) {
            src += "&dpi=" + dpi;   
    }
    return src;  
        });
    }
  
}