如何通过HTML表单将XML发布到服务器? [英] How to post XML to server thru HTML form?

查看:199
本文介绍了如何通过HTML表单将XML发布到服务器?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

 < some_parameters>我必须从HTML表单发送数据到服务器。 
< firstname> Homer< / firstname>
< lastname> Simpson< / lastname>
< street> 74 Evergreen Tr。< / street>
< / some_parameters>

我所知道的是一个在不同域上运行的CRM应用程序。现在我不确定什么是最好的方法来做到这一点。

当用户提交表单时,我正考虑在表单中封装字段的值。因此,如果用户在firstname字段中键入Homer并单击提交,我的JS会将该字段的值更改为< firstname> Homer< / firstname> ,然后发布数据。

如果有帮助,我在客户端使用jQuery。我认为必须有更好的方式,因为我的解决方案会打破JS禁用,似乎有点狡猾,所以如果你能指出我的方向真的很棒。

解决方案

我能想到的最好方法是拦截表单提交操作,并将表单细节转换为XML格式,然后将其提交给服务器。有很多方法可以做到这一点,但最简单的方法是通过像jQuery这样的框架来实现一个解决方案:



这个例子可以在< a href =https://web.archive.org/web/20160305210731/http://www.docunext.com/blog/2009/01/convert-form-data-to-xml-with-jquery.html rel =nofollow noreferrer> http://www.docunext.com/...data-to-xml-with-jquery 它利用 JSON to XML Plugin

  $(#myform)。submit(function(){
var formjson = $('#myform')。serializeArray();
$ b $ .post(/ collect.php,{'data':formxml},function(data){
//回调逻辑
});
return false;
});


I have to post data from my HTML form to server in xml format, something like:

<some_parameters>
    <firstname>Homer</firstname>
    <lastname>Simpson</lastname>
    <street>74 Evergreen Tr.</street>
</some_parameters>

All I know is it goes to one of the CRM applications run on different domain. Now I'm not sure what is the best way to do this.

I was thinking of just wrapping values of fields in my form when user submits the form. So if user typed "Homer" in "firstname" field and clicks submit, my JS would change the value of the field to <firstname>Homer</firstname> and then post the data.

If it helps I'm using jQuery on client side. I think there must be the better way as my solution would break with JS disabled and seems a bit dodgy so if you could point me in the right direction that would be awesome.

解决方案

The best way I can think of is to intercept the form-submit action, and convert the form details into XML format, and then submit that to the server. There are many ways to do this, but the easiest would be to implement a solution via a framework like jQuery:

An example of this very thing can be found online at http://www.docunext.com/...data-to-xml-with-jquery which utilizes the JSON to XML Plugin:

$("#myform").submit(function(){
  var formjson = $('#myform').serializeArray();
  var formxml = json2xml(formjson);
  $.post("/collect.php", { 'data': formxml }, function(data){ 
    // callback logic
  });
  return false;
});

这篇关于如何通过HTML表单将XML发布到服务器?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆