13

I want to Post an XML structure via AJAX to get a filtered result set. The webservice is able to handle post requests, but something seems to be wrong with my POSTing.

$.ajax({
    url: ajaxurl,
    data: {
        inputxml: escape('<test></test>') <- how to post xml structure correctly?
    }, 
    type: 'POST',
    contentType: "text/xml",
    dataType: "text",
    success : parse,
    error : function (xhr, ajaxOptions, thrownError){  
        alert(xhr.status);          
        alert(thrownError);
    } 
}); 

XML:

<?xml version="1.0" encoding="UTF-8"?>
<f:filterChain
        xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance"
        xmlns:f="urn:foo">
    <f:filter attributeId="number">
        <f:rangeCondition conditionSign="INCLUSION" operator="BETWEEN">
            <f:low>5</f:low>
            <f:high>15</f:high>
        </f:rangeCondition>
    </f:filter>
</f:filterChain>

Thanks

7
  • and you are receiving xml response? Commented Sep 8, 2011 at 8:55
  • have you tried the answer? do you get any errors? Commented Sep 8, 2011 at 10:47
  • yeah getting still the http 500 error, internal server error, i'll add the xml trying to post.. wait for edit. thanks (now :)) Commented Sep 8, 2011 at 10:51
  • 1
    you will have to escape the quotes etc see this answer stackoverflow.com/q/3099369/413670, also try sending a simple xml like "<test></test>" se if it gets posted Commented Sep 8, 2011 at 11:11
  • my result is still empty but the ajax post is working and running into the success function... thats a good step. thanks, anything else to take care of? like mime-type or something? Commented Sep 8, 2011 at 11:24

2 Answers 2

37
$.ajax({
    url: ajaxurl,
    data: "<test></test>", 
    type: 'POST',
    contentType: "text/xml",
    dataType: "text",
    success : parse,
    error : function (xhr, ajaxOptions, thrownError){  
        console.log(xhr.status);          
        console.log(thrownError);
    } 
}); 

see this SO answer it may help

jQuery ajax post to web service

Sign up to request clarification or add additional context in comments.

Comments

-1

Maybe it's best to set your values in an object and send that object over to the server as xml by setting the dataType of the ajax method to 'xml'.

2 Comments

Just create an object that you send as data like { 'test' : 'test' }
will jquery automatically convert an object to xml, if you set the data as xml?

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.