2

I have a simple controller function like this:

<HttpPost>
Function SaveXML(payload As String) As Boolean
    If payload IsNot Nothing AndAlso payload.Length > 0 Then
        Return True
    Else
        Return False
    End If
End Function

Which I'm calling from JavaScript like this:

function SaveXML() {

    var payload = '<?xml version="1.0" encoding="utf-8"?><data>XML_GOES_HERE</data>';

    // Calls controller correctly but data is null
    $.ajax({
        url: "/Data/SaveXML/",
        type: "POST",
        processData: false,
        contentType: "text/xml",
        data: payload
    })
    .done(function () { alert('Application saved.'); })
    .fail(function () { alert('Application failed to save.'); });

}

I'm using the example on the JQuery documentation as a base with some advice from here, here, and here. I've tried it with and without processData: false and it makes no difference.

When the call comes in to the Controller method the payload is null. If I post a simple string using some very similar code everything works fine. What precisely needs to be done in order to post XML to a Controller via $.ajax? Is it at the JavaScript or Controller end that the problem lies?

3 Answers 3

1

I eventually managed to find some hints on this and ended up with the following code:

$.ajax({
    url: "/Data/SaveXML/",
    type: "POST",
    processData: false,
    contentType: "application/json; charset=utf-8",
    data: JSON.stringify({ payload: payload })
})
.done(function () { alert('Application saved.'); })
.fail(function () { alert('Application failed to save.'); });

The crucial differences are that the contentType is set to application/json, the data is turned into an object which is then run through the JSON.stringify method to make sure that the various characters that are unsuitable for a querystring can be sent without failing.

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

Comments

1

The default model binding doesn't work with processData set to false. If ServerXml is a string of XML, removing this should make it work:

function SendXmlToServer(ServerXml) {
   $.ajax({ url: "/Home/XmlData",
       type: "POST",
       data: { ResXml: ServerXml }, dataType: "xml",
       success: function () {
           alert("Successful");
           return false;
       }
   });
}

You'll also have to add the ValidateInput attribute to your action method, because normally "HTML markup" isn't allowed:

[HttpPost]
[ValidateInput(false)]
public ActionResult XmlData(string ResXml) 
{   
    return null;
}

Alternatively, you could use custom model binding to seamlessly deserialize the XML, as explained in this blog post url https://lostechies.com/jimmybogard/2011/06/24/model-binding-xml-in-asp-net-mvc-3/.

Comments

0

I believe you may need to name the parameter you are passing to the controller.

So, something like...

var data = '<?xml version="1.0" encoding="utf-8"?><data>XML_GOES_HERE</data>';

$.ajax({
    url: "/Data/SaveXML/",
    type: "POST",
    processData: false,
    contentType: "text/xml",
    data: { payload: data }
})

2 Comments

I'm a little confused, in your other question, this appears to have been answered, and solved?
If you check again there's a distinct difference between that and this even though they use very similar JavaScript. Here I'm asking how to send XML whereas the other question only sends a simple string.

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.