简体   繁体   English

无法从.net中的Ajax调用.asmx web-service

[英]Not able to call .asmx web-service from Ajax in .net

I have an existing .net solution which uses windows authentication. 我有一个使用Windows身份验证的现有.net解决方案。 Now i added another project for Web-service in existing solution and created a .asmx service there. 现在,我在现有解决方案中添加了另一个Web服务项目,并在那里创建了一个.asmx服务。 Through ajax i am trying to call that web-service as below 通过ajax我试图将该网络服务称为如下

Ajax request 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")          
    }
});

and here is my .asmx service 这是我的.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";
    }
}
}

In the above code, i am getting error as No web service found at: /HelloService/HelloData.asmx. 在上面的代码中,我收到错误,因为No web service found at: /HelloService/HelloData.asmx. in "Global.asax.cs" file in "Application_error" method. 在“Application_error”方法中的“Global.asax.cs”文件中。 What's wrong here and what needs to be done? 这里有什么不对,需要做什么?

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. 它已经在代码中显示,为了从Javascript调用ASMX Web服务,您需要添加System.Web.Script.Services.ScriptService属性。

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. 请注意,现在应用了System.Web.Script.Services.ScriptService属性。

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM