简体   繁体   English

如何从Servlet发送json数据响应到特定的Url(Server)?

[英]How to send the json data response to the particular Url(Server) from Servlet?

I have the following code which is with json format data as input variables in the html form. 我有以下代码,其中json格式的数据作为html形式的输入变量。

out.write("<html><body onload=\"document.forms[0].submit()\">\n");
      out.write("<form  enctype='application/json' method=\"POST\" action=\"" + resourcePath + "\">\n");
      out.write("<input name='sessionId' value=\"" + sessionId + "\"/>\n");
      out.write("<input name='sessionValid' value=\"" + true + "\"/>\n");
      out.write("</form>\n</body>\n</html>\n");

I have googled about this and i got the above format but it is not working, still it is taking as a normal string while reading after form has been submitted. 我已经对此进行了谷歌搜索,但是得到了以上格式,但它无法正常工作,但在提交表单后仍在读取时仍将其作为普通字符串。 I have seen same type of questions in stack overflow also but they are directing me to use jquery which i do not want to use. 我也曾在堆栈溢出中看到过相同类型的问题,但它们指示我使用我不想使用的jquery。

What might be the wrong in the above code? 上面的代码可能是什么错误?

Its very hard to tell with the current example given, it would seem there is more of a syntax error or possibly some issue with the browser or submitting to the server side. 给出的当前示例很难说清,似乎有更多的语法错误,或者浏览器或提交到服务器端可能出现问题。

Here is a bare basic example that can be tested if you have PHP... just create a test.php file or something and use the following code. 这是一个简单的基本示例,如果您有PHP,可以进行测试...只需创建一个test.php文件或其他内容,然后使用以下代码即可。 If this works, then it would be the variables used or the way they are being created/echoed in place. 如果可行,则可能是所使用的变量或就地创建/回显它们的方式。

<?php
    if (isset($_REQUEST['sessionId'])) {
        echo "<pre>".print_r($_REQUEST, true)."</pre>";
        exit;
    }
?>
<form enctype='application/json' method="POST">
    <input name='sessionId' value="has7ft8f7w56ef78wte"/>
    <input name='sessionValid' value="true"/>
    <button type='submit'>Submit</button>
</form>

If you can provide some more information, i'll do what i can to help out in this case.. Some times when things dont work.. best to go back to basics for testing then see where it breaks. 如果您可以提供更多的信息,在这种情况下,我会尽力提供帮助。.有时候事情不起作用..最好回到基础进行测试,然后看看有什么坏处。

EDIT 编辑

<form enctype='application/json' method="POST" action="myurl_or_where_ever">
    <input name='sessionId' value="has7ft8f7w56ef78wte"/>
    <input name='sessionValid' value="true"/>
    <button type='submit'>Submit</button>
</form>

By testing with something as simple as this, you're making sure the basic concept works.. If this works, then change the action="" first.. if that works, then change the sessionId/Valid.. The button is to simply rule out the onload event.. This is part of debugging and will save you alot of time in the future. 通过这样简单的测试,您可以确保基本概念有效。如果有效,则首先更改action =“”。如果有效,则更改sessionId / Valid。按钮是只需排除onload事件即可。这是调试的一部分,将来可以节省很多时间。

Replace "myurl_or_where_ever" with the expected value of "resourcePath" 将“ myurl_or_where_ever”替换为“ resourcePath”的期望值

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM