jQuery 如何通过 HTML 表单将 XML 发布到服务器?

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

How to post XML to server thru HTML form?

jqueryhtmlxmlforms

提问by spirytus

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

我必须以 xml 格式将数据从我的 HTML 表单发布到服务器,例如:

<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.

我所知道的是,它适用于在不同域上运行的 CRM 应用程序之一。现在我不确定这样做的最佳方法是什么。

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.

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

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.

如果有帮助,我会在客户端使用 jQuery。我认为必须有更好的方法,因为我的解决方案会在禁用 JS 的情况下中断,而且看起来有点狡猾,所以如果你能指出我正确的方向,那就太棒了。

采纳答案by Sampson

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:

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

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

这件事的一个例子可以在http://www.docunext.com/...data-to-xml-with-jquery在线找到,它利用了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;
});

回答by Darin Dimitrov

Posting XML without javascript or browser plugins is impossible. The two possible formats of posting html forms are application/x-www-form-urlencodedand multipart/form-data.

在没有 javascript 或浏览器插件的情况下发布 XML 是不可能的。发布 html 表单的两种可能格式是application/x-www-form-urlencodedmultipart/form-data

回答by user2700463

I just got this to work in chrome, the key is having the blank space in the text area name:

我刚刚让它在 chrome 中工作,关键是在文本区域名称中有空格:

<html>
    <body>
        <form action="http://target_webservice" method="post">
            <textarea rows="20" cols="100" name=" ">
                <?xml version="1.0"?><requestElements><blah></blah></requestElements>
            </textarea>
            <input type="submit" value="Submit">
        </form>        
    </body>
</html> 

回答by Pierre

You can send a XML using XFORMS. For example see: http://www.mozilla.org/projects/xforms/

您可以使用XFORMS发送 XML 。例如见:http: //www.mozilla.org/projects/xforms/

回答by kmuenkel

If servers-side code is an option, you could use a custom php CURL script as a middleman to forward your request on to the third party in an actual xml format. I'm not sure if CURL comes with a standard php installation, and if it's not an option, you could probably use fsocketopen instead (though personally I think that tactic is harder). But CURL is easy enough to install and extremely useful for basically allowing php to send requests as if it's a browser. The difference that you may be interested in here, is that it does actually allow you to set the header 'Content-type: text/xml'.

如果服务器端代码是一个选项,您可以使用自定义 php CURL 脚本作为中间人,以实际的 xml 格式将您的请求转发给第三方。我不确定 CURL 是否带有标准的 php 安装,如果它不是一个选项,您可能可以使用 fsocketopen 代替(尽管我个人认为这种策略更难)。但是 CURL 很容易安装,并且对于基本上允许 php 像浏览器一样发送请求非常有用。您可能对此感兴趣的不同之处在于,它实际上允许您设置标题“Content-type: text/xml”。

So have your html form send off some regular GET or POST values to your php script. Then have that personal php script convert them into the XML format that the 3rd party is expecting. (Don't forget to precede it with the <?xml version="1.0" encoding="ISO-8859-1"?>tag, with whatever attribute values are appropriate for you.) And then send it off via this code:

因此,让您的 html 表单向您的 php 脚本发送一些常规的 GET 或 POST 值。然后让该个人 php 脚本将它们转换为第 3 方期望的 XML 格式。(不要忘记在<?xml version="1.0" encoding="ISO-8859-1"?>标签前面加上适合您的任何属性值。)然后通过以下代码将其发送出去:

$ch = curl_init();
curl_setopt($ch, CURLOPT_URL, $url);
curl_setopt($ch, CURLOPT_HEADER, 1);
curl_setopt($ch, CURLOPT_SSL_VERIFYPEER, 0);
curl_setopt($ch, CURLOPT_SSL_VERIFYHOST, 0);
curl_setopt($ch, CURLOPT_RETURNTRANSFER, 1);
curl_setopt($ch, CURLOPT_CUSTOMREQUEST, 'POST');
curl_setopt($ch, CURLOPT_POST, 1);
curl_setopt($ch, CURLOPT_POSTFIELDS, $xmlRequest);
curl_setopt($ch, CURLOPT_HTTPHEADER, array(
    'Content-type: text/xml', 
    'Content-length: '.strlen($xmlRequest),
));
$output = curl_exec($ch);
curl_close($ch);