1

I have following Ajax code for GET and DELETE use-cases in scalajs-react.

DELETE:

val ajax = Ajax("DELETE", "http://localhost:8081/delete/"+id)
      .setRequestContentTypeJsonUtf8
      .send("")
      .onComplete { xhr =>
        xhr.status match {
          case 200 => {
              println("Success")
              ....more code
            )
          }
          case _   => {
             println("Status is"+xhr.status)
             Callback.log(xhr.responseText)
           }
        }
      }

    ajax.asCallback

...GET:

val ajax = Ajax("GET", "http://localhost:8081/fetch/"+id)
      .setRequestContentTypeJson
      .send("")
      .onComplete { xhr =>
        xhr.status match {
          case 200 => {
            println("Success")
            ....more code
          }
          case _   => {
             println("Status is"+xhr.status)
             Callback.log(xhr.responseText)
          }
        }
      }
    ajax.asCallback
  

While GET works as expected with code inside onComplete invoked on request completion, for DELETE this is not the case. For DELETE onComplete code is never invoked on request completion, even though on server side delete action is successful.

Why this difference in behaviour?

Mandroid
  • 6,200
  • 12
  • 64
  • 134

1 Answers1

0

ajax.asCallback has type Callback that should be executed by calling .runNow(). I believe, that your GET query just works fine, because somewhere down the code you do this .runNow() for it, but you don't do it for your DELETE query.

Here are both of your examples work just fine:

https://scastie.scala-lang.org/mikla/GEcLg2OJSLWhjbcagaxZ9g/10 (check browser console output)