3

Ok, so I created a test project just to verify that jQuery AJAX works with asp.net service, and it does no problems. I used a default HelloWorld service created in VS studio. I am calling the service via jQuery like this:

in Default.aspx:

<script language="javascript" type="text/javascript">
    $(document).ready(function() {

        //test web service
        $.ajax({
            type: "POST",
            contentType: "application/json; charset=utf-8",
            url: "TestService.asmx/HelloWorld",
            data: "{}",
            dataType: "json",
            success: function(msg) { alert(msg);},
            error: function (XMLHttpRequest, textStatus, errorThrown) {
                debugger;
            }
        });
    });
</script>

in TestService.asmx

using System;
using System.Collections.Generic;
using System.Linq;
using System.Web;
using System.Web.Services;

namespace WebServiceTestWitJQuery
{
    /// <summary>
    /// Summary description for TestService
    /// </summary>
    [WebService(Namespace = "http://tempuri.org/")]
    [WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
    [System.ComponentModel.ToolboxItem(false)]
    // To allow this Web Service to be called from script, using ASP.NET AJAX, uncomment the following line. 
    [System.Web.Script.Services.ScriptService]
    public class TestService : System.Web.Services.WebService
    {

        [WebMethod]
        public string HelloWorld()
        {
            return "Hello World";
        }
    }
}

I then proceeded and copied everything exactly as it is in my project and it does not work. I get a 500 server error.

I verified following:

  1. web.configs identical
  2. pages identical
  3. service class identical
  4. jquery ajax call identical
  5. I can navigate to http://localhost:3272/TestService.asmx?op=HelloWorld and webservice works fine.

What else?

6 Answers 6

2

Figured it out. When not sure what is happening use Fiddler. It clearly shows that server could not create an instance of the service class because it was in the wrong namespace. I had R# disabled and did not notice that I did not change the namespace of the service. Problem solved.

Sign up to request clarification or add additional context in comments.

1 Comment

Also, if you service is in subfolder, you have to specify full path.
1

I have a couple of guesses as I know a bit about jQuery but not anything about asp.net.

On the jQuery call, you are indicating you want a json response. "Hello World" I would think will be returned as a raw string, not formatted as json. Remove the 'dataType: "json",' param and jQuery should do it's best to determine the received datatype.

I think the way you have written your jQuery code is only compatible with RESTful Web Services, not conventional web services with a WSDL. Although I don't know anything about asp.net, I don't see anything in the naming conventions here you are using REST.

Comments

0

Problem is in your Script URL so you need to Write full URL. Here is your solution. EG: URL:localhost:1111/TestService.asmx/HelloWorld

Comments

0

i solved this. i know too late for answer this question but may be somebody has same problem.

i removed below lines from ajax and 500 internal error disabled.

contentType: "application/json; charset=utf-8",
        data: "{}",
        dataType: "json",

Comments

0

I was receiving this error and it was caused by my WebService not having access to the session state. This is fixed by changing

[WebMethod]
public MyData GetMyData() {...}

to

[WebMethod(EnableSession = true)]
public MyData GetMyData() {...}

See this link for more information : How to use Session state in ASP.NET WebService

Comments

0

I post this, because neither of the top solutions worked for me (VS2015, .Net4.0, ajax call in WebForms exactly like in the question). It worked only after I explicitly authorized the HttpPost in the config file like this :

<system.web>
    <webServices>
     <protocols> 
    <add name="HttpGet" /> -- just for direct testing : delete this line after 
    <add name="HttpPost" />
     </protocols>
     </webServices>
</system.web>

Comments

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service and acknowledge you have read our privacy policy.

Start asking to get answers

Find the answer to your question by asking.

Ask question

Explore related questions

See similar questions with these tags.