【问题标题】:JSON Processing - Sending JSON text string to JSP, how to process in JSPJSON 处理 - 将 JSON 文本字符串发送到 JSP,如何在 JSP 中处理
【发布时间】:2012-06-20 22:54:37
【问题描述】:

将 json 数据发送到另一个 jsp 页面 - 进行真正的测试。

您在我的 jsp 上的文本字段中输入了 JSON 格式的字符串。我通过一个表单请求提交这个,由 jquery 处理处理。它被发送到接收者 JSP。我正在使用以下代码来执行此操作。

    $.ajax({ 
    type:       "POST", 
    url:        "receiver.jsp", 
    data:       term, // This is a formatted JSON string
    success:    function(data, textStatus, jqXHR) { 
                        alert('Success : ' + data); 
                        alert('textStatus : ' + textStatus); 
                        alert('jqXHR : ' + jqXHR); 
                        var jsonJqXHR = JSON.stringify(jqXHR);
                        alert('jsonJqXHR : ' + jsonJqXHR); 
        }, 
    error:function (xhr, ajaxOptions, thrownError){ 
        alert('Error xhr : ' + xhr.status); 
        alert('Error thrown error: ' + thrownError); 
    },
    //complete: alert('complete'),
    dataType:   "text" // xml, json, script, text, html                        
}); 

我的问题是,我如何在接收者 JSP 中选择此 POST 并对其进行处理?我见过 getParameter 等的东西,但我不确定。

【问题讨论】:

    标签: javascript jquery json jsp post


    【解决方案1】:

    我认为将其作为参数发送会很容易:

    data: "jsonData=" + term,
    

    您也可以将其放入带有 name="jsonData" 的表单输入并序列化您的表单:

    data: jQuery("form").serialize(),
    

    然后就可以在receiver.jsp中读取了:

    <%
        String jsonData = request.getParameter("jsonData");
    %>
    

    【讨论】:

    • 感谢@alexey28 的回答。我现在可以访问receiver.jsp 中的数据。我有一个对象,如何访问对象内部的数据?
    • 如果你想了解由 json 表示的对象 - 你需要先反序列化它。您可以为此使用 gson 库。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2014-03-07
    • 2013-12-22
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多