Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/jquery/75.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Javascript 如何在Jquery中通过抛出Web API获取HttpStatusCode.NoContent消息_Javascript_Jquery_Asp.net Mvc_Asp.net Web Api2_Asp.net Ajax - Fatal编程技术网

Javascript 如何在Jquery中通过抛出Web API获取HttpStatusCode.NoContent消息

Javascript 如何在Jquery中通过抛出Web API获取HttpStatusCode.NoContent消息,javascript,jquery,asp.net-mvc,asp.net-web-api2,asp.net-ajax,Javascript,Jquery,Asp.net Mvc,Asp.net Web Api2,Asp.net Ajax,Web Api代码 public HttpResponseMessage Get() { try { List<Customer> customerList = new List<Customer>(); TestDBContext dbconntext = new TestDBContext(); var allKey = ControllerContext.

Web Api代码

public HttpResponseMessage Get()
    {
        try
        {
            List<Customer> customerList = new List<Customer>();
            TestDBContext dbconntext = new TestDBContext();
            var allKey = ControllerContext.Request.GetQueryNameValuePairs();
            var customerCode = allKey.SingleOrDefault(x => x.Key == "CustomerCode").Value;
            var customerName = allKey.SingleOrDefault(x => x.Key == "CustomerName").Value;


            customerList = dbconntext.Customers.
                    Where(x => x.CustomerName.Contains((customerName == null) || (customerName == "") ? x.CustomerName : customerName)
                    && x.CustomerCode.Contains((customerCode == null) || (customerCode == "") ? x.CustomerCode : customerCode))
                    .ToList<Customer>();

            if (customerList != null && customerList.Count > 0)
            {
                return Request.CreateResponse(HttpStatusCode.OK, customerList);
            }
            else
            {
                return Request.CreateResponse(HttpStatusCode.NoContent, string.Format(" No customer is found with supplies values as Customer Code:{0} and Customer Name :{1}",customerCode,customerName));
            }
        }
        catch(Exception ex)
        {
            return Request.CreateErrorResponse(HttpStatusCode.BadRequest, ex.Message);
        }
      //  return customerList;
    }
$scope.Search = function () {

        $http({
            url: "api/customerapi?CustomerCode="+$scope.Customer.CustomerCode+"&&CustomerName="+$scope.Customer.CustomerName,
            method: "get",
            //params: { CustomerName: $scope.SearchCustomerName },
        }).then(function (response) {
               $scope.customers = response.data;
        },
            function (response) {
               alert(response.data);
            });
    }
当HttpStatusCode.NoContent返回时,不知道如何从WebApi获取自定义消息传递 我只获得身份 状态:204和状态文本:“无内容” 那么,我们如何从webAPI获取自定义消息呢

return Request.CreateResponse(HttpStatusCode.NoContent, string.Format(" No customer is found with supplies values as Customer Code:{0} and Customer Name :{1}",customerCode,customerName)

如果您使用的是
无内容(204)
,则表示响应为空。或者需要返回
204
,然后用Javascript处理错误消息,因为JS中应该有客户名称和客户代码。另一个解决方案是将状态代码更改为
200
,然后返回内容。如果您使用Postman,您会收到该消息吗?您好。不建议对内容使用NoContent状态代码。请看这里。您可以使用
HttpStatusCode.InternalServerError
来捕获和
HttpStatusCode.BadRequest
而不是
HttpStatusCode.NoContent
。现在,除了使用
console.log(response)
查找内容值的位置之外。但是,首先要确保它像Martin说的那样出现在响应中:使用postman或浏览器开发工具的网络配置文件。