7

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)

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271

5 Answers5

4

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

Miqdad Ali
  • 6,129
  • 7
  • 31
  • 50
  • I need to use only `json` cant change it.. (restricted) – Mr_Green Dec 27 '12 at 08:33
  • you are using `type: "POST",` in jquery ajax call, so the webservice should allow `post` request you can do it by editing `web.config` file too. please see http://codeclimber.net.nz/archive/2006/12/22/How-to-enable-an-ASP.NET-WebService-to-listen-to-HTTP.aspx – Miqdad Ali Dec 27 '12 at 08:38
  • web.config file of webservice – Miqdad Ali Dec 27 '12 at 09:58
3

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

rahul.deshmukh
  • 580
  • 2
  • 6
  • 23
  • Thanks it worked for me, in my case was working in debug mode but when I published the solution the delete method was not working but I already had the content type, I just needed to add the charset=utf-8 – Cesar Duran Mar 07 '18 at 02:58
3

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.

2

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

Community
  • 1
  • 1
Mr_Green
  • 40,727
  • 45
  • 159
  • 271
0

Add Allow Access All Origin In The Server Side