简体   繁体   English

不允许使用GET 405方法

[英]GET 405 method not allowed

This is my jquery code to call web api 这是我调用Web API的jquery代码

var request = {
    RequestId: "123",
    DeviceId: "ACU-B2-01-R1",
    AccessType: "Unlock",
    LoginId: "tester",
    Password: "tester"
};

$.ajax({
    url: 'http://localhost:55208/api/accesspanel',
    type: 'POST',
    data: JSON.stringify(request),
    dataType: 'jsonp',
    contentType: "application/json;charset=utf-8",
    success: function (data) {
        alert("success");
        alert(JSON.stringify(data));
    },
    error: function (x, y, z) {
        alert(x + '\n' + y + '\n' + z);
    }
});

When I run this code, nothing happens. 当我运行此代码时,什么也没有发生。 Neither the success nor error block gets fired. 成功块和错误块都不会触发。 After checking in the debug console of chrome, this is the error record: 在chrome的调试控制台中签入后,这是错误记录:

GET http://localhost:55208/api/accesspanel?callback=jQuery18203847100134007633_…22,%22LoginId%22:%22tester%22,%22Password%22:%22tester%22}&_=1364916423737 405 (Method Not Allowed) 
    send jquery.min.js:2
    p.extend.ajax jquery.min.js:2
    (anonymous function)

I am, however, able to call my web api method successfully using C# code, which looks like this: 但是,我可以使用C#代码成功调用我的Web api方法,如下所示:

    using (var client = new HttpClient())
    {
        client.BaseAddress = new Uri("http://localhost:55208/");

        var request = new DeviceAccessRequest
        {
            RequestId = Guid.NewGuid().ToString(),
            DeviceId = "ACU/B2/01/R1",
            AccessType ="Unlock",
            LoginId = "tester",
            Password = "tester" ,                    
        };
        var response = client.PostAsJsonAsync("api/accesspanel", request).Result;
        if (response.IsSuccessStatusCode)
        {
            var deviceAccessResponse = response.Content.ReadAsAsync<DeviceAccessResponse>().Result;

        }
    }

And this is my web api method: 这是我的Web API方法:

    [HttpPost]
    public HttpResponseMessage PostDeviceControl(DeviceAccessRequest deviceAccessRequest)
    {

        var deviceAccessResponse = new DeviceAccessResponse(deviceAccessRequest.RequestId)
            {
                Status = "OK"
            };
        var response = Request.CreateResponse<DeviceAccessResponse>(HttpStatusCode.OK, deviceAccessResponse);
        return response;
    }

The reason you are seeing a GET request in your console is because you specified dataType: 'jsonp' which works only with GET requests (the reason for that is because jQuery translates this to a <script> tag pointing to the url you specified). 在控制台中看到GET请求的原因是因为您指定了dataType: 'jsonp' ,它仅适用于GET请求(其原因是jQuery将其转换为指向您指定的url的<script>标记)。 If you want to be doing cross domain AJAX with other verbs than GET you cannot use JSONP. 如果要使用除GET以外的其他动词进行跨域AJAX,则不能使用JSONP。 If you need to use other verbs (such as POST in your case) you have 2 options: 如果您需要使用其他动词(例如POST),则有两种选择:

  • CORS . CORS Take a look at the following video which illustrates how you could enable CORS on your Web API. 观看following video ,该following video说明了如何在Web API上启用CORS。 Obviously for this to work your client side browser need tio support it 显然,要使其正常工作,您的客户端浏览器需要tio支持
  • Server side bridge on your domain. 域上的服务器端网桥。 The idea here is to have some server side script on the domain hosting your javascript code that will send the HTTP request to the API and return the response to the client. 这里的想法是在托管您的javascript代码的域上有一些服务器端脚本,该脚本会将HTTP请求发送到API并将响应返回给客户端。 Then you will send a regular AJAX POST request to your own domain 然后,您将向您自己的域发送常规的AJAX POST请求

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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