Update 2
After carefully reading the doc about ajax and the data property Here it says that "Object must be Key/Value pairs", so therefore I can not just pass any object but must wrapp it in some kind of associative array, or JSON, which can be something like this {theResp:leXmlDoc}, so therefore contentType:"XML" does really do anything am I correct?
Hi I would like to send data via AJAX to my ASMX webservice. By that I mean want to send data with the contentType: "application/xml", "text/xml" or "xml". I cannot seem to find the right way to format my XML so that my AJAX can work. If I wrap it inside a JSON then I am able to send it without any problem. So I was wondering is there a specific way I am suppose to form my XML? Here is m AJAX: A few notes I tries passing leXmlDoc as a XMl source string and as an Dom Document object.
function sendToServer(leXmlDoc) {
console.log($.isXmlDoc(leXmlDoc)); //Here I check if leXmlDoc is an XML Document, I
also tries using jsut the XML source string
$.ajax({
type: 'POST',
processData: false, //I also tried setting it to true
contentType: "xml", //I also tried application/xml, and text/xml
dataType: "xml",
url: "/Webservices/TransferXmlData.asmx/SendingXmlToServer",
data: leXmlDoc,
success: function (data) {
console.log(data);
},
error: function () {
console.log("there is a problem sending the XML");
}
});
};
My webservice, I know this works because when I send a JSON it return me the data I sent it. Unless it needs to be different to accept XML.
<WebMethod(EnableSession:=True)> _
Public Function SendingXmlToServer(ByVal theResp As String) As String
Return theREsp
End Function
EDIT
The output of console.log(leXmlDoc) as the XML source string, I check and it is of type string
<?xml version="1.0" encoding="UTF-8" ?>
<Sections>
<Section>
<TheGreeting>Hello</TheGreeting>
<ThePlanet>World</ThePlanet>
<Puncuation>!</Puncuation>
</Section>
</Sections>
Here is the out put as a Dom Document, I check using console.log($.isXmlDoc(leXmlDoc)) and the result is true, typeOf is an object and I created the leXMLDoc by this bit of code where theXmlFile is the XML source string from above:
var domParser = new DOMParser();
var XmlDOM = domParser.parseFromString(theXmlFile, "application/xml");
sendToServer(XmlDOM);
The output of console.log(leXmlDoc) as a dom Document is this
#document
<Sections>
<Section>
<TheGreeting>Hello</TheGreeting>
<ThePlanet>World</ThePlanet>
<Puncuation>!</Puncuation>
</Section>
</Sections>
I hope this helps.
UPDATE 1
When I decided to console.log the xhr like this
error: function (xhr, ajaxOptions, thrownError) {
console.log(xhr);
console.log(xhr.status);
console.log(ajaxOptions);
console.log(thrownError);
}
and open up the object I get an error that says
responseText: "System.InvalidOperationException: Request format is invalid: xml
↵ at System.Web.Services.Protocols.HttpServerProtocol.ReadParameters()
↵ at System.Web.Services.Protocols.WebServiceHandler.CoreProcessRequest()
↵"
So it seems that the format is worng. Yet when I go directly to the webservice by navigating to like this
http://localhost:52530/WebServices/TransferXmlData.asmx/
And manually invoke the webservice with the output of console.log(leXmlDoc (Source string)), and then copying and pasting it works fine.