user3263484
user3263484

Reputation: 11

Posting/receiving JSON object

So I am using this code to post:

     $.ajax({
                url: '/validator/ValidationController/validate',
        data : JSON.stringify(parameters),
        contentType : 'application/json',
        type : 'POST'
    })

And this to receive, both being triggered by the same button press. But it's just getting a null object.

def validate(){
    Thread.currentThread().sleep(1000)
    def parameters = request.JSON;
    assert parameters.a == "a_value";

}

When I hover over the validate function, it's referred to as: Object validator.ValidationController.validate() I'm assuming the url is wrong.

Note: I am pretty inexperienced with this, speaking to me like I'm 5 is encouraged!

Upvotes: 1

Views: 75

Answers (1)

Nick Barnes
Nick Barnes

Reputation: 151

We would need more code to see how you are sending and receiving but it seems like you are not handling the asynchronous aspect. In simple terms, you are sending out the request and immediately trying to handle the response, even though it is not back yet and even though you tried to sleep. Check out the complete config for the ajax function. Something like this:

$.ajax({
    url: '/validator/ValidationController/validate',
    data : JSON.stringify(parameters),
    contentType : 'application/json',
    type : 'POST',
    complete: function(request, success){
        validate(request)
    }
})

def validate(request){
   Thread.currentThread().sleep(1000)
   def parameters = request.JSON;
   assert parameters.a == "a_value";

}

Upvotes: 1

Related Questions