C#Ajax无法从控制器响应中获取数据

时间:2015-09-07 07:29:59

标签: javascript c# jquery ajax

我对ajax有疑问。 我有网址:http://localhost:57295/api/Formgetstatus/id=admin&password=test123!&orderNo=000016-150000012

当我点击此网址时,浏览器会显示已回复的信息:

<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获取数据。请帮我!谢谢大家!

1 个答案:

答案 0 :(得分:0)

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

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

所以响应类型设置为xml