7

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

错误

加载资源失败:服务器响应状态为 405(不允许方法)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'但现在还有另一个错误。

未捕获的 SyntaxError:意外的令牌 <
:12015/myWebService.asmx?op=GetCategories&callback=jQuery183010907560377381742_1356599550427&{}&_=1356599550438:3

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

有关的

Link1(解释)
Link2(已解决)

4

5 回答 5

4

试试这个

 [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

于 2012-12-27T08:25:37.740 回答
3

将你的内容类型设为“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);
 }
});
});

也参考链接

于 2012-12-27T09:03:59.823 回答
2

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

 jQuery.support.cors = true;

现在它工作正常:)

仅在IE浏览器中

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

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

于 2012-12-27T09:48:57.523 回答
2

就我而言,它失败了,因为servet中没有POST方法,但我有GET方法。

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

于 2016-01-29T14:25:56.660 回答
0

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

于 2019-10-22T14:02:36.927 回答