Take the 2-minute tour ×
Stack Overflow is a question and answer site for professional and enthusiast programmers. It's 100% free, no registration required.

I tried creating a simple asp.net web api and tried calling it using jQuery. But it throws a 404 error. Here is the web api code

public class ProductController : ApiController
{
    [HttpPost]
    public Product GetProductDetail()
    {
        Product product = new Product();
        product.ProductName = "Cricket Bat";
        product.ProductPrice = "100";

        return product;
    }
}

And here is the jQuery Ajax code:

$(function () {
        $.ajax({
            type: 'GET',
            url: 'Controllers/ProductController/GetProductDetail',
            success: function (success) {
                console.log(success);
            },
            error: function (error) {
                console.log(error);
            }
        });
    });
share|improve this question
    
change type: 'GET' to type: 'POST' in $.ajax() function and replace "Controllers/ProductController/GetProductDetail" by "/Product/GetProductDetail" and it would be better if you will use [HttpGet] for getting data –  Sergey Boiko Aug 1 '14 at 8:34
    
tried both GET and POST but both not working... –  iJade Aug 1 '14 at 8:35
    
@SergeyBoiko still not working. Throwing error POST http://localhost:49826/Product/GetProductDetail 404 (Not Found) in console –  iJade Aug 1 '14 at 8:38
    
could you please show your routes? –  Sergey Boiko Aug 1 '14 at 8:39
2  
don't know why someone has given you a -1, so +1 to balance it out. It isn't "wrong" but just not following standard design patterns. If you are not following a RESTful design pattern, then you would might as well just use a WebService –  Tim B James Aug 1 '14 at 8:55

1 Answer 1

Try decorating your method with HttpGet from System.Web.Http and Not System.Web.Mvc

public class ProductController : ApiController
{
    [System.Web.Http.HttpGet]
    [System.Web.Http.AcceptVerbs("GET", "POST")]
    public Product GetProductDetail()
   {
       Product product = new Product();
       product.ProductName = "Cricket Bat";
       product.ProductPrice = "100";

       return product;
   }
}

Then change your ajax url to "/Product/GetProductDetail"

share|improve this answer

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.