ixx
ixx

Reputation: 32269

Ajax request with Play executes success and error (with undefined parameters)

I'm trying to get an Ajax request working with Play 2.1/Scala and JQuery. I followed: http://www.playframework.com/documentation/2.1.x/ScalaJavascriptRouting

My controller gets called with the correct parameters. But in Javascript, both success and error callbacks are executed and all parameters are undefined.

The template:

@(param: String)(implicit req: RequestHeader)

@helper.javascriptRouter("jsRoutes")(
    routes.javascript.MyController.myMethod
)

@main("Welcome to Play 2.0") {...}

The javascript function, which is somewhere in this template:

function myFunction(param1, param2, param3) {

    jsRoutes.controllers.MyController.myMethod(param1, param2, param3).ajax({
        success: new function(data, textStatus, jqXHR) {
            console.log("ajax success data: " + data);
            console.log("ajax success textStatus: " + textStatus);
            console.log("ajax success jqXHR: " + jqXHR);

        }, error: new function(jqXHR, textStatus, errorThrown) {
            console.log("ajax error xhr: " + jqXHR);
            console.log("ajax error textStatus: " + textStatus);
            console.log("ajax error errorThrown: " + errorThrown);

        }
    });

The controller:

def myMethod(param1:String, param2:String, param3:String) = Action { implicit request =>

    Ok(Json.obj({"key" -> "sdfsdfs"}))

 }

I debugged this and I can see in the browser's network monitor the request and the response, which is of type JSON and contains the correct data.

But this is the output of my callbacks:

ajax success data: undefined localhost:9000/:101
ajax success textStatus: undefined localhost:9000/:102
ajax success jqXHR: undefined localhost:9000/:103
ajax error xhr: undefined localhost:9000/:106
ajax error textStatus: undefined localhost:9000/:107
ajax error errorThrown: undefined 

I have no idea, I also looked here Scala JavaScript Routing Play Framework and couldn't find the reason. I also don't see errors in the server or in the browser.

Thanks.

Upvotes: 0

Views: 586

Answers (1)

ixx
ixx

Reputation: 32269

I found it! I was using "new" in the callback functions. Without it:

success: function(data, textStatus, jqXHR) {//...
}, error: function(jqXHR, textStatus, errorThrown) {//...
}

It works correctly.

Upvotes: 1

Related Questions