【问题标题】:Ajax request with Play executes success and error (with undefined parameters)带有 Play 的 Ajax 请求执行成功和错误(带有未定义的参数)
【发布时间】:2013-08-23 15:51:58
【问题描述】:

我正在尝试使用 Play 2.1/Scala 和 JQuery 获取 Ajax 请求。我关注了:http://www.playframework.com/documentation/2.1.x/ScalaJavascriptRouting

使用正确的参数调用我的控制器。但是在 Javascript 中,成功和错误回调都会执行,并且所有参数都是未定义的。

模板:

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

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

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

这个模板中某处的javascript函数:

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);

        }
    });

控制器:

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

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

 }

我对此进行了调试,我可以在浏览器的网络监视器中看到请求和响应,它是 JSON 类型并且包含正确的数据。

但这是我的回调的输出:

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 

我也不知道,我也看了Scala JavaScript Routing Play Framework这里也找不到原因。我也没有在服务器或浏览器中看到错误。

谢谢。

【问题讨论】:

    标签: ajax scala playframework-2.1


    【解决方案1】:

    我找到了!我在回调函数中使用了“new”。没有它:

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

    它工作正常。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 2011-06-17
      • 1970-01-01
      • 2018-09-13
      • 1970-01-01
      • 2017-10-21
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多