Quantcast
Channel: Web API
Viewing all articles
Browse latest Browse all 4850

Get data of Web Api is returning in Ajax Error function

$
0
0

Hi,

I have created one Web API and published in local IIS and using it in other MVC Application in Jquery Ajax function the issue is that the Data is returning in Ajax error function , below is the code 

[Route("api/Item/GetItembyId")]
         [HttpGet]
        public HttpResponseMessage GetItembyId(int id)
        {

            try
            {
                List<ItemViewModel> itm = new List<ItemViewModel>();
                return Request.CreateResponse(HttpStatusCode.Found,itm = db.Items.Where(x => x.ItemID == id).Select(x=> new ItemViewModel{
                    ItemDescription = x.ItemDescription,
                    ItemPrice = x.ItemPrice,
                    ItemID = x.ItemID
                }).ToList());
            }

            catch
            {
                return Request.CreateResponse(HttpStatusCode.NotFound, "Item Not Found");
            }
        }

I can execute these code in browser successfully by using this url http://localhost/api/Item/GetItembyId?id=3

I was trying to use this Get Web Api in other MVC Application in JQuery Ajax function and it is returning the data in error  function of Ajax not success function

below is the script

 var url = "http://localhost/api/Item/GetItembyId?id=" + Id;$.ajax({

            type: "GET",
            url: url,
            data: {},

            success: function (data, textStatus, xhr) {
                console.log(data);
            },
            error: function (response) {

                console.log(response.responseText);
               
        })


Viewing all articles
Browse latest Browse all 4850

Trending Articles



<script src="https://jsc.adskeeper.com/r/s/rssing.com.1596347.js" async> </script>