0

I have this code :

$.ajax({
    type:'POST',
    url:'/store',
    processData: false,
    contentType: false,
    data: formData,
    async: false,
    success: function (response) {

        if(JSON.parse(response).status != 'Failure'){
            console.log(JSON.parse(response).comment)
            widget.attr("data-selection-id", JSON.parse(response).comment.id)
            removeSelection()

        }

    }
});

This code started working after adding async: false. It was strange because sometimes the attribute was added and sometimes it was not. Even if I tried to perform another action on the element, such as .hide(), the effect was the same. I also tried, for example, displaying the message with the ID from the response, and that always worked correctly. As I wrote before - at the moment it works, but disabling async makes the application run terribly slow, and this is a problem for me. Do you have any other ideas how I can solve this problem?

I would like that every time after send request, the returned id is stored as a data attribute, but without async:false

Reyno
  • 6,119
  • 18
  • 27
Hello123G
  • 3
  • 3
  • You should check this: https://stackoverflow.com/questions/1478295/what-does-async-false-do-in-jquery-ajax – Weedoze Mar 02 '23 at 15:25
  • From the very limited amount of code provided, I would guess that the variable `widget` changes outside the call - and changes between the ajax starting and completing. – freedomn-m Mar 02 '23 at 16:01
  • Note also that changing the data via `.attr("data-..", newvalue)` will not have an effect if the data attribute has already been read into data. You should use `widget.data("selection-id"...` - but of course, that depends on how widget works and if it's been coded properly or not. – freedomn-m Mar 02 '23 at 16:02

0 Answers0