views:

57

answers:

1

My program is giving me an error from jquery. I don't understand why. I works in c# but in jquery it does not

using (XmlTextWriter writer = new XmlTextWriter(new StringWriter(sb)))
            {
                writer.Formatting = System.Xml.Formatting.Indented;
                ser.Serialize(writer, ct);
                XMLContent = sb.ToString();
            }
            return Content(XMLContent, System.Net.Mime.MediaTypeNames.Text.Xml);

f

$.ajax(
        {
            type: "POST",
            url: action,
            data: formobj,
            dataType: "xml",
            success: function(result) {
                alert(result);
            },
            error: function(req, status, error) {
                alert(req.statusText);
            }
        });
        return false;

When I replace

return Content(XMLContent, System.Net.Mime.MediaTypeNames.Text.Xml);

with

return Content(XMLContent);

and remove

dataType: "xml",

from jquery it all works.

+2  A: 

One way that could simplify and optimize your ajax a lot, is to use JSON instead of XML.

(unless you really want your result to be XML)

In ASP.Net MVC you can let your Action return Json as result.

return Json(new {
    variableName: someData,
    anotherVariableName: someMoreData
});

In your Js:

$.post(
    'yourActionName',
    optionalData,
    function(d) {
        alert(d.variableName);
        alert(d.anotherVariableName);
    }
);

Couldn't be more simple than this :)

Mickel