【问题标题】:Getting String value sent by $.Ajax获取 $.Ajax 发送的字符串值
【发布时间】:2015-12-18 20:03:25
【问题描述】:

我正在尝试使用 request.getParameter(); 在我的 jsp 中获取我的 Ajax 调用提交的值;但它一直给null,这是我的$.Ajax

function get(){ 
var select = document.getElementById("model");
var selectedString = select.options[select.selectedIndex].value;
alert(selectedString);
$.ajax({
    type:"POST",
    url:"index.jsp",
    data:{fram:selectedString},
    dataType:"text",
    success:function(data){
    alert("data loaded: " );
}});
}

这就是触发 get();

<select id="model" class="form-control"  name="From"style="background:#FCDFD5;" onchange="get()">
<% 
while(r.next()){
%>
<option><%=r.getString(3)%></option>
<% } %></select>

警报有效,但当我尝试打印我的 jsp 中的值时它显示为 null,这是我的 request.getParameter();

<% String name=request.getParameter("fram");
    out.println(name);
%>

提前非常感谢

【问题讨论】:

  • alert("data loaded successfully" ); 怎么能说null
  • selectedString 的值是多少?
  • selectedString 必须在 javascript 中给它一个值。请更新您的帖子,显示您如何设置此变量以及它在 javascript 中的定义位置
  • selectedString 是下拉列表中的选定值
  • 除非它被设置为全局变量或与 ajax 调用在同一范围内,否则在您在此处发布的脚本中为 null

标签: jquery ajax jsp


【解决方案1】:

我将把这个留在答案中:seems like the same problem as this thread。 Ajax 不是提交表单,而是发送 JSON 请求。 Request.getParameter 需要表单 POST 或 url 参数,从 ajax 作为正文内容提交的 JSON 会混淆它,即使它只是一个字符串而不是复杂类型。这不是 getParameter 正在寻找的地方。

【讨论】:

猜你喜欢
  • 1970-01-01
  • 1970-01-01
  • 2017-01-07
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 1970-01-01
  • 2019-12-28
  • 1970-01-01
相关资源
最近更新 更多