【问题标题】:Parameter value is getting null in Ajax?参数值在 Ajax 中为空?
【发布时间】:2014-07-12 10:12:03
【问题描述】:

我正在将一个参数从 html 发送到 jsp。当我试图在 jsp 中获取值时,我得到的是空值。我的代码有什么问题?大家有什么建议,

该值在警报中的 java 脚本中打印

HTML:

<!DOCTYPE html>
<html>
<head>
<script>
function load()
{
var cal=document.getElementById('course').value;
alert(cal); // Value is printing here
var xmlhttp;
if (window.XMLHttpRequest)
  {
  xmlhttp=new XMLHttpRequest();
  }
    xmlhttp.onreadystatechange=function()
  {
  if (xmlhttp.readyState==4 && xmlhttp.status==200)
    {
    document.getElementById("myDiv").innerHTML=xmlhttp.responseText; // I am gtting response but value is null
    }
  }
xmlhttp.open("POST","LoadAjax.jsp",true);
xmlhttp.send("cal="+cal);
}
</script>
</head>
<body>
<div id="myDiv"><h2>Submit</h2></div>
<input type = "text" id = "course" name = "course">
<button type="button" onclick="load()">Change Content</button>
</body>
</html>

我的 Jsp 代码如下,我可以发送响应,但唯一的问题是我没有得到值

<%@ page language="java" contentType="text/html; charset=ISO-8859-1"
    pageEncoding="ISO-8859-1"%>
    <%@ page import ="java.sql.*" %>
<%@ page import ="javax.sql.*" %>

<%
String sn=request.getParameter("cal");
System.out.println(sn);
    %>
    <select class="input_dropdown01" name="ccode" id="Div1" onchange="load();"  >
    <option selected="selected">Select</option>
    <option value="<%out.println(sn);%>" ><%out.println(sn);%></option>
    </select>
    <%
%>

【问题讨论】:

  • 您必须使用 Jquery。它非常容易编码
  • 使用&lt;%=sn%&gt; 而不是&lt;%out.println(sn);%&gt; 即赋值。
  • @Braj 你的第一个表达式无效,应该改成&lt;%=sn%&gt;

标签: javascript html ajax jsp


【解决方案1】:

确保您点击的 URL 有效。使用浏览器开发工具查看发送的内容。

这对我有用:

您必须相应地提取值。 我认为您也可以在您的 url 中将 cal 作为查询字符串传递

var obj = {};
    obj.cal = "Your value here";
    var postBody = "";
    postBody = JSON.stringify(obj);

    var url = "server_url";

    xmlHttp.open("POST", url, true);
    xmlHttp.setRequestHeader("Accept", "application/json");
    xmlHttp.setRequestHeader("Content-type", "application/json");
    xmlHttp.send(postBody);


OR
        var url = "server_url";
        xmlHttp.open("POST", url, true);
        xmlHttp.setRequestHeader("Accept", "application/json");
        xmlHttp.setRequestHeader("Content-type", "application/json");
        xmlHttp.send("Your_data_here");

【讨论】:

    【解决方案2】:

    在使用前请使用转义函数

    xmlhttp.send("cal="+ escape(cal));

    应该是

    xmlhttp.send("cal="+ escape(cal));

    因为 excape 对特殊字符进行编码

    http://www.w3schools.com/jsref/jsref_escape.asp

    和服务器端解码它使用

    Server.UrlDecode

    http://www.aspsnippets.com/post/2009/02/01/AJAX-Calls-Using-JavaScript-And-XMLHTTP.aspx

    【讨论】:

      【解决方案3】:

      我忘记设置标题了,现在可以工作了

      xmlhttp.open("POST","LoadAjax.jsp",true);
      xmlhttp.setRequestHeader('Content-Type', 'application/x-www-form-urlencoded');
      xmlhttp.send("cal= "+cal);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2015-06-15
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2019-07-09
        相关资源
        最近更新 更多