简体   繁体   中英

C# Ajax can't get data from controller response

I have a question about ajax. I have URL : http://localhost:57295/api/Formgetstatus/id=admin&password=test123!&orderNo=000016-150000012

When i click this URL then browser display information responsed :

<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({
        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. Please help me! Thanks everyone!

The return value from your controller action should be something that derives from ActionResult. Try converting your object to an xml string then returning

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

so the response type is set to xml

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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