【问题标题】:C# Ajax can't get data from controller responseC# Ajax 无法从控制器响应中获取数据
【发布时间】:2026-02-03 10:00:01
【问题描述】:

我有一个关于 ajax 的问题。 我有网址:http://localhost:57295/api/Formgetstatus/id=admin&password=test123!&orderNo=000016-150000012

当我单击此 URL 时,浏览器会显示响应信息:

<FormGetStatusRespond xmlns:i="http://www.w3.org/2001/XMLSchema-instance" xmlns="http://schemas.datacontract.org/2004/07/FormService.RestApi">
<ResultInfo>
<ErrorInfo i:nil="true"/>
<ErrorType>None</ErrorType>
<Status>Ok</Status>
</ResultInfo>
<Status>OK</Status>
<StatusCode>1</StatusCode>
</FormGetStatusRespond>

这是控制器响应:

public FormGetStatusRespond GetStatus(string id, string password, string orderNo)
        {
            var respond = new FormGetStatusRespond();
            var resultInfor = new ResultInfo();
            var errorInfor = new ErrorInfo();

            if(!this.AuthenticateForUser(id, password))
            {
                // Result Infor
                resultInfor.Status = WebApiStatus.Error;
                resultInfor.ErrorType = WebApiErrorType.AuthenticationError;

                // Error Infor
                errorInfor.Messsage = "abc";

                resultInfor.ErrorInfo = errorInfor;

                respond.ResultInfo = resultInfor;

                return respond;
            }

            var orderDal = new OrderRepository();
            var orderModel = orderDal.FindByOrderNo(orderNo);

            if(orderModel != null)
            {
                // Result Infor
                resultInfor.Status = WebApiStatus.Ok;
                resultInfor.ErrorType = WebApiErrorType.None;
                respond.ResultInfo = resultInfor;
                respond.Status = this.GetOrderStatus(orderModel.OrderStatus);
                respond.StatusCode = ((int)orderModel.OrderStatus).ToString();
            }
            else
            {
                // Result Infor
                resultInfor.Status = WebApiStatus.Error;
                resultInfor.ErrorType = WebApiErrorType.ApplicationError;

                // Error Infor
                errorInfor.Messsage = "abc:" + orderNo + "abc";

                resultInfor.ErrorInfo = errorInfor;

                respond.ResultInfo = resultInfor;

                return respond;
            }

            return respond;
        }

我使用 ajax 来获取 XML 数据:

$.ajax({
        type: 'GET',
        url: "http://localhost:57295/api/Formgetstatus/id=admin&password=test123!&orderNo=000016-150000012",
        dataType: 'xml',
        success: function (data) {
          alert('b');
        },
        error: function (error) {
          alert('a');
        }
      });

我不知道为什么我不能从 ajax 获取数据。请帮我!谢谢大家!

【问题讨论】:

  • 结果如何?您看到警报“a”还是“b”?如果是 b,那么“数据”对象中有什么?而不是对错误对象执行 alert('b') 尝试 alert(JSON.stringify(data)) 。让我们看看你得到了什么。
  • 使用浏览器中的 F12 工具查看实际响应。它是在顶部包含 标记,还是您希望的纯 xml?
  • 当我使用 ajax 时,它是错误的。我希望数据对象中的结果响应是 xml 数据。
  • 你好 GeekyMonkey!这是 alert(JSON.stringify(error)) : { "readyState":0, "responseText":"", "responseXML":null, "status":0, "statusText":"error" }
  • 好的。所以问题出在服务器端。您可以发布 FormGetStatusRespond 类的源代码吗?它的基类是什么?

标签: javascript c# jquery ajax


【解决方案1】:

控制器操作的返回值应该是从 ActionResult 派生的。尝试将您的对象转换为 xml 字符串,然后返回

return this.Content(xmlString, "text/xml");

所以响应类型设置为xml

【讨论】: