即使我打算POST,Kendo UI Grid请求方法也是OPTIONS

时间:2013-07-25 03:01:17

标签: ajax asp.net-web-api kendo-ui kendo-grid kendo-asp.net-mvc

我尝试将数据从Kendo Ui Grid发布到我的web api

var crudServiceBaseUrl = "http://127.255.0.1:8081/api",
        dataSource = new kendo.data.DataSource({
            transport: {
                read:  {
                    url: crudServiceBaseUrl + "/meeting",
                    dataType: "jsonp",
                },
                update: {
                    type: "POST",
                    url: crudServiceBaseUrl + "/meeting/postmeeting",
                    contentType: "application/json; charset=utf-8",
                    dataType: 'json',

                    success: function (msg) {
                        alert("success");
                    }
                },

          .....


    $("#grid").kendoGrid({
        dataSource: dataSource,
        pageable: true,
        toolbar: ["create"],
        columns: [
            { field: "Organizer", title: "Organizer" },
            { field: "Title", title: "Title" },
            { field: "Location", title: "Location", },
            { command: ["edit", "destroy"], title: " ", width: "160px" }],

        editable: "inline"
    });

但是当我更新时

我收到405错误

Request URL:http://127.255.0.1:8081/api/meeting/postmeeting
Request Method:OPTIONS
Status Code:405 Method Not Allowed
Request Headersview source
Accept:*/*
Accept-Encoding:gzip,deflate,sdch
Accept-Language:en-US,en;q=0.8
Access-Control-Request-Headers:accept, origin, content-type
Access-Control-Request-Method:POST
Host:127.255.0.1:8081
Origin:http://localhost:30320
Proxy-Connection:keep-alive

有人可以向我解释请求方法如何成为OPTION? 因此,我的ASP.NET Web API中的POST方法不会调用

  [HttpPost]
  public HttpResponseMessage PostMeeting(MeetingEntity meeting)
    {
        _meetingRepository.Update(meeting);
        return new HttpResponseMessage(HttpStatusCode.OK);
    }

1 个答案:

答案 0 :(得分:1)

如果您从http://127.255.0.1:8081/api/meeting/postmeeting访问http://localhost:30320,则它是跨源请求。默认情况下,浏览器会阻止访问不同来源的资源。检查this

跨源资源共享(CORS)是解决此限制的一种方法。由于您执行POST,浏览器会发出预先发出的CORS请求,这基本上是一个OPTIONS请求。如果此OPTIONS请求导致具有必要CORS头的响应,则浏览器将随后发出POST请求。您可以实现一个操作方法来处理OPTIONS,但现在很容易在Web API中启用CORS。请检查此link