2012-07-31 61 views
3

我对HTML5开发非常非常新,这个问题可能非常愚蠢,但我找到了一个答案(或者我已经搜索得非常好)。发送html表单通过发布到webservice

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

我有两个问题:

  1. 我该怎么命名的形式领域?如果我试图发送userName,我认为我必须将此测试作为ID放置到保存该值的字段中。
  2. 这是因为我好奇。哪些是发送到Web服务的邮件内容?

这是我发现互联网搜索的例子:我想我会需要这些ID,而在Web服务处理他们获取这些值

<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 

,不是吗?

+0

好了,这一切都依赖于Web服务。 如果它是[REST API] [1],那么您可以将其发布到它。 [1]:http://en.wikipedia.org/wiki/Representational_state_transfer – netigger 2012-07-31 09:22:39

+0

对不起,我不明白你:我该如何命名表单域? – VansFannel 2012-07-31 09:24:01

+0

这取决于web服务和web服务的规范。你想要连接什么web服务? – netigger 2012-07-31 09:25:10

回答

2

这取决于,你可以做一个张贴到页面的重定向(在.NET中,你会这样处理):

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

再挑这个了在服务器端脚本在使用postpage.ashx

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

你也可以使用jQuery作出AJAX调用到S使用以下AME页:

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 
    } 
}); 

您将处理在服务器端代码相同的方式邮寄。这里要注意的关键是,无论您输入的名称是名称您输入元素的属性是什么将作为键/值对发送到您的接收URL。

+0

你可以给一个exmaple如何检索键/值对在(ASP.NET)webmethod?我有它: [WebMethod(Description =“测试方法。”)] public void TestMethod(String APP_VERSION_CODE) { // APP_VERSION_CODE应该是值还是...?我不知道.. } 但我不断收到ReportSenderException – Zuop 2015-06-26 14:56:38

0

每个web服务都应该给你类似WSDL的东西,它通常包含你可以使用的可用字段和方法的规范。如果您正在连接的web服务的URL为webservice.com,则尝试使用webservice.com/wsdl以获得WSDL

检查这个话题:click

+0

我在问我该如何发送这些字段。我想我必须在形式上设置一些东西来识别它们。顺便说一下,Web服务尚未实现。 – VansFannel 2012-07-31 09:22:03

+0

请看这里:http://stackoverflow.com/questions/8753098/how-get-html-post-data-and-pass-to-a-webservice – mnmnc 2012-07-31 09:24:57

0

属性“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> 

为了访问这些数据,你会做这样的事情在Servlet:

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