【问题标题】:the server responded with a status of 405 (Method Not Allowed)服务器响应状态为 405(不允许使用方法)
【发布时间】:2012-12-12 15:38:31
【问题描述】:

我是 Web 服务的新手,当我尝试在 Chrome 控制台中运行我的页面(本地)时遇到以下错误

错误

加载资源失败:服务器响应状态为 405 (Method Not Allowed) http://localhost:12015/myWebService.asmx?op=GetCategories


相关代码如下:

jQuery

 $.ajax({
     url: "http://localhost:12015/myWebService.asmx?op=GetCategories",
     type: "POST",
     ------------
     ------------
    success: function (data) {                    
         var categories = data.d;
         $.each(categories, function (index, category) {
             category.CategoryId).text(category.CategoryName);
         });
    },
    error: function (e) {  //always executing 'error' only
         alert("hello");
    }

网络服务网址

http://localhost:12015/myWebService.asmx


 [WebMethod]
 [ScriptMethod(ResponseFormat = ResponseFormat.Json)]
 public List<Categories>  GetCategories()
 {
      //code
 }

页面网址

http://localhost:11761/Default.aspx

编辑:当我刚刚包含 dataType: 'jsonp' 时,错误消失了,但现在又出现了另一个错误。

Uncaught SyntaxError: Unexpected token :12015/myWebService.asmx?op=GetCategories&amp;callback=jQuery183010907560377381742_1356599550427&amp;{}&amp;_=1356599550438:3

当我点击链接(在错误中提到)时,它正在显示页面。那么可能是什么问题?我不知道错误的含义(以及要显示的代码部分)。请帮忙。

相关

Link1(解释)
Link2(已解决)

【问题讨论】:

标签: jquery asp.net web-services


【解决方案1】:

试试这个

 [WebMethod]
 [ScriptMethod(UseHttpPost = true)]
 public List<Categories>  GetCategories()
 {
      //code
 }

或编辑web.config

<system.web>
    ...
    <webServices>
        <protocols>
              <add name="HttpSoap"/> 
              <add name="HttpPost"/> --> 
              <add name="HttpGet"/>
              <add name="Documentation"/>
              <add name="HttpPostLocalhost"/>
        </protocols>
    </webServices>
    ...
</system.web>

参考http://codeclimber.net.nz/archive/2006/12/22/How-to-enable-an-ASP.NET-WebService-to-listen-to-HTTP.aspx

【讨论】:

  • 我只需要使用json不能改变它..(受限)
  • 您在 jquery ajax 调用中使用 type: "POST",,因此 web 服务应该允许 post 请求,您也可以通过编辑 web.config 文件来实现。请看codeclimber.net.nz/archive/2006/12/22/…
  • webservice的web.config文件
【解决方案2】:

将你的内容类型设为“application/json; charset=utf-8”,如下

$(document).ready(function() {
$.ajax({
type: "POST",
url: "RSSReader.asmx/GetRSSReader",
data: "{}",
contentType: "application/json; charset=utf-8",
dataType: "json",
success: function(msg) {
  // Hide the fake progress indicator graphic.
  $('#RSSContent').removeClass('loading');

  // Insert the returned HTML into the <div>.
  $('#RSSContent').html(msg.d);
 }
});
});

也可以参考link

【讨论】:

  • 谢谢它对我有用,在我的情况下是在调试模式下工作,但是当我发布解决方案时,删除方法不起作用但我已经有了内容类型,我只需要添加 charset= utf-8
【解决方案3】:

我在 jquery ajax 调用上方我朋友的建议添加了一个简单的行

 jQuery.support.cors = true;

现在一切正常:)

仅在 IE 浏览器中

我很高兴知道它是否可以使用不同的方式解决,因为它不是recommended

无论如何,经过多次努力,我再次问了这个问题,我得到了不同的错误,在这篇文章中解决了 here

【讨论】:

    【解决方案4】:

    在我的情况下,它失败了,因为servet 中没有 POST 方法,但我有 GET 方法。

    所以我只是更改了类型:“GET”,所以它现在可以工作了。

    【讨论】:

      【解决方案5】:

      在服务器端添加允许访问所有源

      【讨论】:

      • 您应该将此作为评论而不是答案。
      猜你喜欢
      • 1970-01-01
      • 2014-05-01
      • 2014-12-01
      • 2015-08-04
      • 2016-08-09
      • 1970-01-01
      • 2017-10-19
      相关资源
      最近更新 更多