Ajax POST到WCF Rest CORS兼容的WebService引发错误405 [英] Ajax POST to WCF Rest CORS-compliant WebService throws error 405

查看:234
本文介绍了Ajax POST到WCF Rest CORS兼容的WebService引发错误405的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我正在对WCF REST WebServices进行一些测试,并且无法进行POST调用. 我创建了一个Web服务,该服务公开了有关优良的Northwind DB的一些测试数据,并且由于我希望从测试HTML页面本地使用它,并且由于我想测试CORS功能,因此请按照以下说明使其符合CORS要求指令 http://enable-cors.org/server_wcf.html .

I'm doing some test over WCF REST WebServices and i'm stuck with the POST call. I've created a webservice that exposes some test data about the good ol' Northwind DB and since i wish to consume it locally from a test HTML page and since i'd like to test CORS capabilities, i made it CORS compliant by following these instruction http://enable-cors.org/server_wcf.html.

不幸的是,我拨打POST电话时出现问题. 与GET调用不同(效果很好),POST调用会引发此错误:

Unfortunately problems comes out when i make POST calls. Unlike GET calls (works very well), POST call throws this error:

这到底是什么?似乎"Access-Control-Allow-Origin"标头未在客户端进行正确管理,这是因为在我的EnableCrossOriginResourceSharingBehavior WCF类中,方法"ApplyDispatchBehavior"(它过滤了到达请求的"Access-Control-Allow-Origin"标头) )在我进行POST呼叫时被命中,但随后Ajax呼叫失败.

What the hell is it? it seems that "Access-Control-Allow-Origin" header is not correctly managed client-side, beacuse in my EnableCrossOriginResourceSharingBehavior WCF class, the method "ApplyDispatchBehavior" (it filter "Access-Control-Allow-Origin" headers of the arrival requests) is hit when i make a POST call, but then Ajax call fails.

这是我的jQuery Ajax发布命令:

This is my jQuery Ajax post command:

       //Create new object
        var item = {
            "CustomerId": "0",
            "CompanyName": "prova"
        };

        //Push object
        $.ajax({
            type: "POST",
            url: 'http://localhost:3434/NorthwindService.svc/Customer/Create',
            crossDomain: true,
            headers: {'Access-Control-Allow-Origin' : '*'},
            data: JSON.stringify(item),
            success: function (data) {
                alert('ok!');
            },
            contentType: 'application/json; charset=utf-8',
            dataType: 'json'
        });

是我的WCF服务Visual Studio 2013项目. 要对其进行测试,只需将web.config中的"NorthwindConnectionString"设置为现有的.我遇到的Web服务方法是" http://localhost的POST: 3434/NorthwindService.svc/Customer/Create "方法,其他所有方法都可以正常工作. 这是我的方法合约的预览:

This is my WCF service Visual Studio 2013 project. To test it, you only have to set "NorthwindConnectionString" in web.config to an existing one. The webservice method that i've problem with, is the POST to the "http://localhost:3434/NorthwindService.svc/Customer/Create" method, all the others works fine. This is a preview of my method contract:

 [OperationContract]
 [WebInvoke(Method = "POST", RequestFormat = WebMessageFormat.Json, UriTemplate = "Customer/Create", BodyStyle=WebMessageBodyStyle.WrappedRequest)]
 void NewCustomer(CustomerDTO customer);

提前谢谢.

推荐答案

我不知道发生了什么,但是由于supertopi和他的链接,我采取了正确的步骤使它可行.不幸地实现了此处如何处理的所有内容具有WCF自托管功能的Ajax JQUERY POST请求无法正常工作.即使创建一个新项目,我仍然会收到"405方法不允许"的信息. 在我看来,唯一有效的方法如下:

I don't know what's going on, but thanks to supertopi and his link, i did the right steps to make it works. Unfortunately implementing all things discussed in here How to handle Ajax JQUERY POST request with WCF self-host did't works. I continued to get "405 Method not allowed" even by creating a new project. The only thing that works in my case is the following:

1)实现CustomHeaderMessageInspector和EnableCrossOriginResourceSharingBehavior类,并编辑 http://enable- cors.org/server_wcf.html .

1) Implement CustomHeaderMessageInspector and EnableCrossOriginResourceSharingBehavior classes and edit web.config as exposed in http://enable-cors.org/server_wcf.html.

2)在服务合同中创建以下方法:

2) Create in the service contract the following method:

[OperationContract]
[WebInvoke(Method = "OPTIONS", UriTemplate = "*")]
 void GetOptions();

3)将其实现为空.

public void GetOptions()
{

}

听起来很疯狂,但实际上有效. 如果我删除GetOptions()操作合同,则我的客户端继续出现405错误.如果我按照supertopi的链接所示(显然是在删除了步骤1中创建的所有内容之后)实现了它,那么它也不起作用.

It sounds crazy, but it actually works. If i remove GetOptions() operation contract, i continue to get 405 error on my client. If i implement it like indicated by supertopi's link (obviously after remove all stuff created in the step 1), it doesn't work either.

希望有帮助.

这篇关于Ajax POST到WCF Rest CORS兼容的WebService引发错误405的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆