通过 post 将 html 表单发送到 webservice

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/11736775/
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

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-08-29 01:58:15  来源:igfitidea点击:

send html form via post to webservice

formshtmlpost

提问by VansFannel

I'm very very new on HTML5 development and this question could be very silly but I've found an answer for it (or I've searched very well).

我对 HTML5 开发非常陌生,这个问题可能非常愚蠢,但我已经找到了答案(或者我已经很好地搜索了)。

I want to send a form to a web service via post (I don't want to show all fields in URL).

我想通过邮寄将表单发送到 Web 服务(我不想显示 URL 中的所有字段)。

I have two question:

我有两个问题:

  1. How must I named forms fields? If I trying to send an userNameI think I have to put this test as ID to the field which will held that value.
  2. And this is because I'm so curious. Which is the post message content which is sent to web service?
  1. 我必须如何命名表单字段?如果我尝试发送一个,userName我想我必须将此测试作为 ID 放入将保存该值的字段中。
  2. 这是因为我很好奇。哪个是发送到网络服务的帖子消息内容?

This is an example that I've found searching Internet:

这是我在 Internet 上找到的一个示例:

 <FORM action="http://somesite.com/prog/adduser" method="post">
    <P>
    <LABEL for="firstname">First name: </LABEL>
              <INPUT type="text" id="firstname"><BR>
    <LABEL for="lastname">Last name: </LABEL>
              <INPUT type="text" id="lastname"><BR>
    <LABEL for="email">email: </LABEL>
              <INPUT type="text" id="email"><BR>
    <INPUT type="radio" name="sex" value="Male"> Male<BR>
    <INPUT type="radio" name="sex" value="Female"> Female<BR>
    <INPUT type="submit" value="Send"> <INPUT type="reset">
    </P>
 </FORM

I think I will need those ids to get those values while processing them on web service, isn't it?

我想在 Web 服务上处理它们时,我需要这些 id 来获取这些值,不是吗?

采纳答案by Paul Aldred-Bann

It depends, you could do a post to a page with a redirect (in .NET you would handle it this way):

这取决于,您可以使用重定向对页面进行发布(在 .NET 中,您可以这样处理):

<form action="http://myurl/postpage.ashx" method="post">
    <input name="forename" />
    <input name="surname" />
    <input type="submit" value="Submit" />
</form>

And then pick this up in the server side script at postpage.ashxusing:

然后在postpage.ashx的服务器端脚本中使用:

string forename = Request["forename"];
string surname = Request["surname"];

You could also use jQuery to make an ajax call to the same page using the following:

您还可以使用 jQuery 使用以下命令对同一页面进行 ajax 调用:

var forename = $("input[name=\"forename\"]").val();
var surname = $("input[name=\"surname\"]").val();

$.ajax({
    url: "http://myurl/postpage.ashx",
    type: "POST",
    async: true, // set to false if you don't mind the page pausing while waiting for response
    cache: false,
    dataType: "json",
    data: "{ 'forename': '" + forename + "', 'surname': '" + surname + "' }",
    contentType: "application/json; charset=utf-8",
    success: function(data) {
        // handle your successful response here
    },
    error: function(xhr, ajaxOptions, thrownError) {
        // handle your fail response here
    }
});

You would handle the post in the server side code the same way. The key thing to note here is that whatever you enter as the nameattribute of your input element is what will get POSTed as a key/value pair to your receiving URL.

您将以相同的方式处理服务器端代码中的帖子。这里要注意的关键是,无论您输入什么作为input 元素的name属性,都会作为键/值对发布到您的接收 URL。

回答by Marko Letic

Attribute "name" is the one that needs to be unique in order to pass that parameter to a Servlet (or wherever). The post method then encrypts the message and sends it to the Servlet.

属性“name”是唯一的属性,以便将该参数传递给 Servlet(或其他任何地方)。post 方法然后加密消息并将其发送到 Servlet。

<form method="post" action = "LoginServlet">
     Name: <input type="text" name="userName">
     Password: <input type="password" name="password"> 

    <input type="submit" name = "Login" class="button">
</form>

In order to access that data you will do something like this in the Servlet:

为了访问该数据,您将在 Servlet 中执行以下操作:

String userName = request.getParameter("userName");

回答by mnmnc

every web service should give you something like WSDLwhich normally contains specification of available fields and methods you can use. if the webservice you are connecting to have url webservice.comthan try webservice.com/wsdlto get the WSDL.

每个 Web 服务都应该为您提供类似WSDL 的东西,它通常包含您可以使用的可用字段和方法的规范。如果您要连接的网络服务有 urlwebservice.com比尝试webservice.com/wsdl获取WSDL

Check this topic: click

检查此主题:单击