[英]Pass POST Parameters with Jquery POST to Javascript from Java Servlet
From my JS I am starting a POST Request on my Servlet 从我的JS我正在我的Servlet上启动POST请求
$.post("RecipeServlet",
{
r_id: r_id,
},
function(data, status){
var foo =data.foo
var bar =data.bar
});
And my Servlet is now supposed to do something with the r_id and should now pass results to my JS, since I need to pass Arrays aswell as simple Strings I thought I would need JSON and do it kinda like this: 现在,我的Servlet应该对r_id进行处理,并且应该将结果传递给JS,因为我需要传递数组以及简单的字符串,我认为我需要JSON并像这样进行:
response.setContentType("application/json");
PrintWriter out = response.getWriter();
out.print(new Gson().toJson(bar));
out.print(new Gson().toJson(foo));
How can I access the data correctly? 如何正确访问数据? I would like to access it like "data.bar" as you can see in the example JS.
正如您在示例JS中看到的那样,我想像“ data.bar”一样访问它。 So my main question is probably, how do I manage to make the JSON Objects accessable by something like data.foo?
因此,我的主要问题可能是,如何使data.foo之类的对象可以访问JSON对象?
You need to specify the data type by adding it at the end of the $.post() 您需要通过在$ .post()的末尾添加数据类型来指定数据类型
Example: 例:
$.post( "RecipeServlet", { r_id: r_id }, function( data ) {
var foo = data.foo
var bar = data.bar
}, "json");
Source: http://api.jquery.com/jquery.post/ 来源: http : //api.jquery.com/jquery.post/
声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.