jQuery 服务器以 405 状态响应(方法不允许)
声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow
原文地址: http://stackoverflow.com/questions/14051432/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me):
StackOverFlow
the server responded with a status of 405 (Method Not Allowed)
提问by Mr_Green
I am new to Web Sevice, I am getting the following error when I tried to run my page (Local) in Chrome Console
我是 Web 服务的新手,当我尝试在 Chrome 控制台中运行我的页面(本地)时出现以下错误
ERROR
错误
Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
http://localhost:12015/myWebService.asmx?op=GetCategories
加载资源失败:服务器响应状态为 405(方法不允许)
http://localhost:12015/myWebService.asmx?op=GetCategories
Here is the related code:
下面是相关代码:
jQuery
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");
}
web service URL
网络服务网址
http://localhost:12015/myWebService.asmx
http://localhost:12015/myWebService.asmx
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Categories> GetCategories()
{
//code
}
Page URL
网页网址
http://localhost:11761/Default.aspx
http://localhost:11761/Default.aspx
EDIT: The error gone when I just included dataType: 'jsonp'
But now there is another error.
编辑:当我刚刚包含时错误消失了dataType: 'jsonp'
但现在还有另一个错误。
Uncaught SyntaxError: Unexpected token <
:12015/myWebService.asmx?op=GetCategories&callback=jQuery183010907560377381742_1356599550427&{}&_=1356599550438:3
未捕获的语法错误:意外的标记 <
:12015/myWebService.asmx?op=GetCategories&callback=jQuery183010907560377381742_1356599550427&{}&_=1356599550438:3
When I clicked the link(which was mentioned in the error), it is displaying the page. Then what could be the problem ? I dont know what the error means (and also which part of code to show). Please help.
当我点击链接(在错误中提到)时,它正在显示页面。那么可能是什么问题?我不知道错误是什么意思(也不知道要显示哪一部分代码)。请帮忙。
Related
有关的
采纳答案by Mr_Green
I added a simple line on my friend's suggestion above the jquery ajax call
我在 jquery ajax 调用上方添加了我朋友的建议的简单行
jQuery.support.cors = true;
Now it is working fine :)
现在它工作正常:)
ONLY IN IEBROWSER
仅在IE浏览器中
I would be happy to know if it can be solved using different way as it is not recommended.
我很高兴知道它是否可以使用不同的方式解决,因为不推荐这样做。
Anyhow I asked this question again after many efforts and I got different error which was solved in this post here
无论如何,我再次问这个问题,经过多次努力,我这是在这篇文章中解决了不同的错误这里
回答by Miqdad Ali
try this
尝试这个
[WebMethod]
[ScriptMethod(UseHttpPost = true)]
public List<Categories> GetCategories()
{
//code
}
or edit web.config
或编辑 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>
回答by rahul.deshmukh
make your content type as "application/json; charset=utf-8", as follows
使你的内容类型为“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);
}
});
});
also refer link
也参考链接
回答by user5857614
in my case it was failing becasue of there was not POST method in servet, but I had GET method.
就我而言,它失败了,因为服务器中没有 POST 方法,但我有 GET 方法。
so I jsut changed the type:"GET", so it is working now.
所以我只是改变了类型:“GET”,所以它现在可以工作了。
回答by Cáptáin Mosa
Add Allow Access All Origin In The Server Side
在服务器端添加允许访问所有源