使用ajax调用webapi get方法时出现405错误

时间:2016-06-15 06:42:39

标签: ajax asp.net-web-api http-status-code-405

我开发了一个WebApi和Client页面进行测试。 这是我的控制器

public class CarDetailsController : ApiController
{
    // GET api/cardetails
    [HttpGet]
    public IEnumerable<CarsStock > GetAllcarDetails()
    {
        CarsStock ST = new CarsStock();
        CarsStock ST1 = new CarsStock();
        List<CarsStock> li = new List<CarsStock>();

            ST.CarName = "Maruti Waganor";
            ST.CarPrice = "4 Lakh";
            ST.CarModel = "VXI";
            ST.CarColor = "Brown";
            li.Add(ST);

            ST1.CarName = "Maruti Swift";
            ST1.CarPrice = "5 Lakh";
            ST1.CarModel = "VXI";
            ST1.CarColor = "RED";
            li.Add(ST1);

        return li;  
    }

}

这是我的ajax电话

    <button onclick="AllcarDetails()"></button>
    <script  type="text/javascript">
        function AllcarDetails()
        {
            $.ajax({  
            type: "GET",  
            headers: {
                'Accept': 'application/json',
                'Content-Type': 'application/json'
            },
            contentType: 'application/json',
            url: "http://localhost:1822/api/cardetails", //URI   
            success: function (data) {  
                debugger;  
                var datadatavalue = data;  

            },  
            error: function (xhr) {  
                alert(xhr.responseText);  
            }  
    });  
        }
    </script>

它始终提供405方法不允许错误。我通过谷歌搜索尝试但无法找到确切的情况。有人可以帮我解决这个问题吗?

从浏览器&#39; http://localhost:1822/api/cardetails&#39;

调用时,结果正确

1 个答案:

答案 0 :(得分:0)

您是否检查过在运行此代码的位置激活HTTP激活?