Невозможно вызвать веб-сервис .asmx из Ajax в .net - PullRequest
0 голосов
/ 29 апреля 2019

У меня есть существующее решение .net, которое использует проверку подлинности Windows.Теперь я добавил еще один проект для Web-сервиса в существующее решение и создал там сервис .asmx.Через ajax я пытаюсь вызвать этот веб-сервис, как показано ниже:

Ajax запрос

$.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")          
    }
});

, и вот мой сервис .asmx

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";
    }
}
}

В приведенном выше коде, я получаю ошибку как No web service found at: /HelloService/HelloData.asmx. в файле «Global.asax.cs» в методе «Application_error».Что здесь не так и что нужно сделать?

1 Ответ

0 голосов
/ 29 апреля 2019

Это уже показано в коде, чтобы вызвать веб-сервис ASMX из Javascript, вам нужно добавить атрибут System.Web.Script.Services.ScriptService.

Вы можете просто раскомментировать строку, поэтому ваш код будет выглядеть следующим образом:

/// <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";
    }
}

Обратите внимание, что атрибут System.Web.Script.Services.ScriptService теперь применяется.

...