如何制作2.1控制器功能执行捕获的外部URL并将json数据对象返回给javascript。
的Javascript
$.ajax({
url: "/documents/getjsontext/" + talksUrl ,
type: 'GET',
data: "",
dataType: "json",
contentType: "application/json; charset=utf-8",
success: function(data){ do_this(data);},
error: function () {alert("Error in Ajax Call");}
});
Route- / documents / acontext /:jsonurl controllers.Class.acontext(jsonurl:String)
public static JSONObject acontext(String jsonurl) {
InputStream is = new URL(jsonurl).openStream();
try {
BufferedReader rd = new BufferedReader(new InputStreamReader(is, Charset.forName("UTF-8")));
String jsonText = readAll(rd);
JSONObject json = new JSONObject(jsonText);
return json;
} finally {
is.close();
}
}
答案 0 :(得分:0)
首先,您需要添加一个JsRoutes类。我把它命名为“myJsRoutes”
public class CommonController extends Controller{
public static Result javascriptRoutes() {
response().setContentType("text/javascript");
return ok(
Routes.javascriptRouter("myJsRoutes",
routes.javascript.Controller_name.function()
);
}
}
现在定义了你的js路由。并且“myJsRoutes”可以进一步用于在你的scala文件中调用:
myJsRoutes.controllers.Controller_name.function_name().ajax({
//your ajax handlers here
});