小编Tho*_*mas的帖子

ASP.NET MVC4 WebAPI和发布XML数据

我错过了一个新的webapi技巧 - 我试图通过一个帖子请求提交一个xml字符串而没有太多运气.

前端使用这样的jQuery:

    $(document = function () {
    $("#buttonTestAPI").click(function () {

        var d = " <customer><customer_id>1234</customer_id></customer>";
        $.ajax({
            type: 'POST',
            contentType: "text/xml",
            url: "@Url.Content("~/api/Customer/")",
            data: d,
            success: function (result) {
                var str = result;
                $("#output").html(str);
            }
        });
    });
});
Run Code Online (Sandbox Code Playgroud)

我的控制器目前非常简单 - 只是后期操作的默认设置 - 尝试返回传入的内容:

    public string Post(string value)
    {
        return value;
    }
Run Code Online (Sandbox Code Playgroud)

但是,"值"重复为空.奇怪的是,当我在jquery中更改我的数据时,就像这样:

d = "<customer_id>1234</customer_id>";
Run Code Online (Sandbox Code Playgroud)

然后我在控制器中得到"值"为1234.

如何访问控制器中更复杂的xml字符串?

xml asp.net-mvc asp.net-mvc-4 asp.net-web-api

12
推荐指数
3
解决办法
2万
查看次数

标签 统计

asp.net-mvc ×1

asp.net-mvc-4 ×1

asp.net-web-api ×1

xml ×1