Light
Light

Reputation: 1677

Send XML data to server using jQuery $.post

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:

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

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

Upvotes: 1

Views: 1118

Answers (2)

Tapas Mahata
Tapas Mahata

Reputation: 351

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.

Upvotes: 0

Rory McCrossan
Rory McCrossan

Reputation: 337580

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.

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

Upvotes: 2

Related Questions