0
votes

I have an existing .net solution which uses windows authentication. Now i added another project for Web-service in existing solution and created a .asmx service there. Through ajax i am trying to call that web-service as below

Ajax request

$.ajax({
    type: "POST",
    url: "HelloService/HelloData.asmx/HelloWorld",
    data: "{parameterList:" + JSON.stringify(model) + "}",
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    async: true,
    cache: false,
    success: function (jsondata) {
      alert(jsondata);
    }, error: function (x, e) {
      alert("Error")          
    }
});

and here is my .asmx service

namespace HelloService
{
/// <summary>
/// Summary description for HelloData
/// </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 HelloData : System.Web.Services.WebService
{

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

In the above code, i am getting error as No web service found at: /HelloService/HelloData.asmx. in "Global.asax.cs" file in "Application_error" method. What's wrong here and what needs to be done?

1
Can I ask why you're creating a new ASMX service in 2019? Why aren't you using Web API instead?mason
Your JavaScript "data: "{parameterList:" + JSON.stringify(model) + "}" is wrong it should be data: '{parameterList: "' + JSON.stringify(model) + '"}' this. and remove async: true, it will excute the next statement even if the responce not came .Abhay Agarwal

1 Answers

0
votes

It is already shown in the code, in order to call ASMX web service from Javascript, you need to add the System.Web.Script.Services.ScriptService attribute.

You can simply uncomment the line, so your code will be like this:

/// <summary>
/// Summary description for HelloData
/// </summary>
[WebService(Namespace = "http://tempuri.org/")]
[WebServiceBinding(ConformsTo = WsiProfiles.BasicProfile1_1)]
[System.ComponentModel.ToolboxItem(false)]
[System.Web.Script.Services.ScriptService]
public class HelloData : System.Web.Services.WebService
{

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

Notice that the System.Web.Script.Services.ScriptService attribute is now applied.