Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

the server responded with a status of 405 (Method Not Allowed)

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)

like image 204
Mr_Green Avatar asked Dec 27 '12 08:12

Mr_Green


2 Answers

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>

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

like image 75
Miqdad Ali Avatar answered Nov 07 '22 03:11

Miqdad Ali


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

like image 28
rahul.deshmukh Avatar answered Nov 07 '22 01:11

rahul.deshmukh