简体   繁体   English

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

[英]C# Ajax can't get data from controller response

I have a question about ajax.我有一个关于ajax的问题​​。 I have URL : http://localhost:57295/api/Formgetstatus/id=admin&password=test123!&orderNo=000016-150000012我有网址: http://localhost:57295/api/Formgetstatus/id=admin&password=test123!&orderNo=000016-150000012

When i click this URL then browser display information responsed :当我单击此 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>

And this is controller response:这是控制器响应:

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;
        }

I use ajax to get data XML :我使用 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');
        }
      });

I don't know why i can't get data from ajax.我不知道为什么我无法从 ajax 获取数据。 Please help me!请帮我! Thanks everyone!谢谢大家!

The return value from your controller action should be something that derives from ActionResult.控制器操作的返回值应该是从 ActionResult 派生的。 Try converting your object to an xml string then returning尝试将您的对象转换为 xml 字符串然后返回

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

so the response type is set to xml所以响应类型设置为 xml

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM