使用jQuery $.post将XML数据发送到服务器。

时间:2022-10-22 22:44:09

I was browsing through other questions and posts but couldn't find the answer.

我正在浏览其他的问题和帖子,但没有找到答案。

I need to send data to PHP file which saves the data to the server. Some of the data is just string variables and one of the variables is XML data. I tried to do it with the following code:

我需要将数据发送到PHP文件,将数据保存到服务器。有些数据只是字符串变量,其中一个变量是XML数据。我试着用下面的代码来做:

$.post(
    "save.php",
    { 
        userId: _UserId, 
        pName: _pName, 
        pId: _pId, 
        xml: $(_xml).find("main").text()
    },
    function () { 
        console.log("Saved"); 
    }
);

So _xml is an XML document and I am trying to save the entire XML in the server. The POST works but the problem is that it saves only the text, without the <> brackets.

所以_xml是一个XML文档,我试图将整个XML保存在服务器中。但问题是它只保存文本,没有<>括号。

How do I properly send the XML data to the server? Any help will be appreciated.

如何正确地将XML数据发送到服务器?如有任何帮助,我们将不胜感激。

2 个解决方案

#1


2  

The POST works but the problem is that it saves only the text, without the <> brackets.

但问题是它只保存文本,没有<>括号。

Try using html() instead. I know the name may be incorrect in this instance, but the underlying method will not remove elements contained within the current.

尝试使用html()。我知道在此实例中名称可能是不正确的,但是底层方法不会删除当前所包含的元素。

xml: $(_xml).find("main").html()

#2


0  

Try this way:

试试这种方法:

$.ajax({
type : "POST",
url : "Save.php",
data : {
    method : "Save",
    userId: _UserId, 
    pName: _pName, 
    pId: _pId, 
    xml: escape(xmlString)
},
dataType : "json",
cache : false,
success : function(data) {
// Process return status data here
}
});

Note: You need to decode the xml string at server side.

注意:您需要在服务器端解码xml字符串。

#1


2  

The POST works but the problem is that it saves only the text, without the <> brackets.

但问题是它只保存文本,没有<>括号。

Try using html() instead. I know the name may be incorrect in this instance, but the underlying method will not remove elements contained within the current.

尝试使用html()。我知道在此实例中名称可能是不正确的,但是底层方法不会删除当前所包含的元素。

xml: $(_xml).find("main").html()

#2


0  

Try this way:

试试这种方法:

$.ajax({
type : "POST",
url : "Save.php",
data : {
    method : "Save",
    userId: _UserId, 
    pName: _pName, 
    pId: _pId, 
    xml: escape(xmlString)
},
dataType : "json",
cache : false,
success : function(data) {
// Process return status data here
}
});

Note: You need to decode the xml string at server side.

注意:您需要在服务器端解码xml字符串。