Reputation: 41852
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)
Upvotes: 7
Views: 141373
Reputation: 31
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.
Upvotes: 3
Reputation: 41852
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
Upvotes: 2
Reputation: 590
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
Upvotes: 3
Reputation: 6147
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>
Upvotes: 4