Warning: file_get_contents(/data/phpspider/zhask/data//catemap/7/wcf/4.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181

Warning: file_get_contents(/data/phpspider/zhask/data//catemap/2/.net/25.json): failed to open stream: No such file or directory in /data/phpspider/zhask/libs/function.php on line 167

Warning: Invalid argument supplied for foreach() in /data/phpspider/zhask/libs/tag.function.php on line 1116

Notice: Undefined index: in /data/phpspider/zhask/libs/function.php on line 180

Warning: array_chunk() expects parameter 1 to be array, null given in /data/phpspider/zhask/libs/function.php on line 181
Asp.net 405 WCF中不允许的方法错误_Asp.net_Wcf - Fatal编程技术网

Asp.net 405 WCF中不允许的方法错误

Asp.net 405 WCF中不允许的方法错误,asp.net,wcf,Asp.net,Wcf,有人能发现这个实现的问题吗?我可以在浏览器中打开它,它可以工作,但是客户端调用(使用jquery和asp.net ajax都失败了) 服务合同 [OperationContract(Name = "GetTestString")] [WebInvoke(Method = "GET", ResponseFormat = WebMessageFormat.Json )] string GetTestString(); 在Web.config和其他绑定中,我有一个webH

有人能发现这个实现的问题吗?我可以在浏览器中打开它,它可以工作,但是客户端调用(使用jquery和asp.net ajax都失败了)

服务合同

[OperationContract(Name = "GetTestString")]
[WebInvoke(Method = "GET",
           ResponseFormat = WebMessageFormat.Json
   )]
string GetTestString();
在Web.config和其他绑定中,我有一个webHttp绑定

<endpoint address="ajax" binding="webHttpBinding" contract="TestService" behaviorConfiguration="AjaxBehavior" />
然后,我将在中使用该方法
要调用服务,链接上的示例使用Http POST,而不是Http GET。这是“不允许的方法”-您需要更改代码以执行GET

  $.ajax({
                type: "POST",.....});
您发布的作为客户端代码源的链接具有以下块:

 $.ajax( { 
                url: url,
                data: json,
                type: "POST",
                processData: false,
                contentType: "application/json",
                timeout: 10000,
                dataType: "text",  // not "json" we'll parse

注意那里的
类型:“POST”
——你的应该是“GET”。我假设您从发布的链接获取JQuery,因为405状态表明您的调用代码是错误的,而不是服务。

对于method not allowed error,您需要检查的只是确保您的http web调用/请求与服务中[WebInvoke…]中指定的相同

  $.ajax({
                type: "POST",.....});
应与服务接口(在“[操作合同]”项下)中指定的内容相同


不知道你的意思。GetTestString具有WebInvoke属性,带有GET选项编辑我的答案以获取更多详细信息(因为代码块不会很好地放在注释中)。谢谢!当我从POST改为进入代理JS时,它开始工作了。你知道作者为什么在从服务获取信息时选择使用POST(我假设它应该是POST)吗?任何web服务都可以选择实现任何Http方法,其中最常见的是
GET
POST
PUT
DELETE
POST
PUT
通常用于写入信息,因此,在这方面,您链接的示例是不寻常的-一个名为
GetStockQuote
的方法作为
POST
实现似乎是一个奇怪的选择-但这是服务作者的选择:-)值得注意的是,您可以使用任何方法返回结果(你可以使用Http
DELETE
来返回信息,如果你愿意的话!)-这不一定有什么意义!我希望我能为此放弃一百万票。错过了这个,我觉得很愚蠢。
 $.ajax( { 
                url: url,
                data: json,
                type: "POST",
                processData: false,
                contentType: "application/json",
                timeout: 10000,
                dataType: "text",  // not "json" we'll parse
  $.ajax({
                type: "POST",.....});
 [WebInvoke(Method = "POST",BodyStyle = WebMessageBodyStyle.Wrapped,ResponseFormat = WebMessageFormat.Json)]