javascript 如何将数据传递给 AJAX 调用
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/18110783/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
How to pass data to AJAX call
提问by AbtPst
Can anyone tell me how to pass data to the jsp making the AJAX call ? This is what I am trying:
谁能告诉我如何将数据传递给进行 AJAX 调用的 jsp?这就是我正在尝试的:
Here is my AJAX call:
这是我的 AJAX 调用:
$.get("gridedit.jsp", { before: "row", time: "2pm" })
.done(function(data) {
alert("Data Loaded: " + data);
});
here is my gridedit.jsp
这是我的 gridedit.jsp
<% String b=request.getParameter("before");
if(b.equalsIgnoreCase("row"))
{
System.out.println("ROW ROW ROW your boat");
out.println("bummer");
} %>
i want to store the value returned from gridedit.jsp into a javascript variable. How should I do this ?
我想将从 gridedit.jsp 返回的值存储到一个 javascript 变量中。我该怎么做?
please help
请帮忙
thanks
谢谢
EDIT:
编辑:
here is what i also tried
这是我也尝试过的
$.ajax({
url: "gridedit.jsp",
async: true,
cache: false,
type:"GET",
data: {
before:'row',
},
error: function(msg) { alert(msg); },
complete: function (xhr, status) { alert('complete: '+status); }
});
i get two alerts, the first one says
我收到两个警报,第一个说
[object][object]
and the second one says
第二个说
error
can anyone figure out whats going on ?
谁能弄清楚发生了什么?
please help
请帮忙
thanks
谢谢
Errors;
错误;
so here i what i tried
所以我在这里尝试过
$.ajax({
url: "gridedit.jsp",
//dataType: "json",
async: true,
cache: false,
type:"GET",
data: {
before:'row'
},
error: function( jqXHR, textStatus, errorThrown ) { alert(jqXHR);
alert(textStatus);
alert(errorThrown);},
complete: function (xhr, status) {
alert('jqXHR:'+xhr);
alert('complete: '+status); }
});
i get the following alerts in order:
我按顺序收到以下警报:
jqXHR: [object][object]
jqXHR: [对象][对象]
testStatus:
测试状态:
parseerror
errorthrown:
错误抛出:
Unexpected end of input
can anyone please help me in solving this ? my gridedit.jsp does this->
谁能帮我解决这个问题?我的 gridedit.jsp 是这样做的->
<%String b=request.getParameter("before");
System.out.println("b is here !" + b);
out.println("HELLO");%>
please help
请帮忙
thanks
谢谢
回答by Shade
Try number two:
尝试第二个:
I have an ajax request that looks like this:
我有一个 ajax 请求,如下所示:
$.ajax({
url: "/someplace",
dataType: "json",
async: true,
cache: false,
data: {
number0: 0,
key: littleKey
},
success: function(data, status)
{
alert(data);
}
});
and it works as is expected.
它按预期工作。
And you can specify get with type : "GET"
in with the other options.
您可以使用type : "GET"
其他选项指定 get with in 。
Maybe try having your .jsp print some data no matter what, and also print what data it is receiving.
也许尝试让您的 .jsp 无论如何打印一些数据,并打印它正在接收的数据。
回答by Shade
It is in a variable, the data variable in the anonymous function passed to .done()
它在一个变量中,匿名函数中的数据变量传递给 .done()
Whatever you need the data returned by gridedit.jsp to do, the easiest way to have anything done with it is to use it in that function. Right now all you are doing with it is making a popup containing the returned data.
无论您需要 gridedit.jsp 返回的数据做什么,处理它的最简单方法就是在该函数中使用它。现在你所做的就是制作一个包含返回数据的弹出窗口。