I am new to Web Sevice, I am getting the following error when I tried to run my page (Local) in Chrome Console
ERROR
Failed to load resource: the server responded with a status of 405 (Method Not Allowed)
http://localhost:12015/myWebService.asmx?op=GetCategories
Here is the related code:
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
[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public List<Categories> GetCategories()
{
//code
}
Page URL
http://localhost:11761/Default.aspx
EDIT: The error gone when I just included dataType: 'jsonp' But now there is another error.
Uncaught SyntaxError: Unexpected token <
: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
Link1 (explanation)
Link2 (solved)
Answers:
Thank you for visiting the Q&A section on Magenaut. Please note that all the answers may not help you solve the issue immediately. So please treat them as advisements. If you found the post helpful (or not), leave a comment & I’ll get back to you as soon as possible.
Method 1
try this
[WebMethod]
[ScriptMethod(UseHttpPost = true)]
public List<Categories> GetCategories()
{
//code
}
or edit 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>
Method 2
make your content type as “application/json; charset=utf-8”, as follows
$(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
Method 3
I added a simple line on my friend’s suggestion above the jquery ajax call
jQuery.support.cors = true;
Now it is working fine 🙂
ONLY IN IE BROWSER
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
Method 4
in my case it was failing becasue of there was not POST method in servet, but I had GET method.
so I jsut changed the type:”GET”, so it is working now.
Method 5
Add Allow Access All Origin In The Server Side
All methods was sourced from stackoverflow.com or stackexchange.com, is licensed under cc by-sa 2.5, cc by-sa 3.0 and cc by-sa 4.0