无法从客户端连接到asp.net上的web方法

本文关键字:net 上的 web 方法 asp 客户端 连接 | 更新日期: 2023-09-27 18:00:56

我正在尝试使用ajax函数从html页面连接到web方法

    $.ajax({
                url: "http://localhost:43599/WebForm1.aspx/GetWeatherResultsbyscript",
                cache: false,
                type: "POST",
                contentType: "application/json; charset=utf-8",
                data:{Zip:input},
                dataType: 'json',
                success: function (data,result) {
                    alert(result.d);
                },
                error: function () { alert(" Server not found"); }
            });
    [System.Web.Services.WebMethodAttribute(), System.Web.Script.Services.ScriptMethodAttribute()]
    private object GetWeatherResultsbyscript(string Zip) {
        ServiceReference1.ForecastReturn result = new ServiceReference1.ForecastReturn();
        ServiceReference1.WeatherSoapClient client = new ServiceReference1.WeatherSoapClient();
        result = client.GetCityForecastByZIP(Zip);
        return (result);

    }

结果,我发现服务器没有找到,但如果我删除ajax函数中的行

 contentType: "application/json; charset=utf-8",
                 dataType: "json",`

结果,我变得"未定义"了。

有人能说出我在这个连接到web方法的含义中做错了什么吗?

无法从客户端连接到asp.net上的web方法

该方法应该是公共的。试试下面的代码,

[WebMethod]
[ScriptMethod(ResponseFormat = ResponseFormat.Json)]
public object GetWeatherResultsbyscript(string Zip) {    
     ServiceReference1.ForecastReturn result = new ServiceReference1.ForecastReturn();
     ServiceReference1.WeatherSoapClient client = new ServiceReference1.WeatherSoapClient();
     result = client.GetCityForecastByZIP(Zip);
     return (result);
 }

此外,

URL中缺少应用程序名称:http://localhost:43599/applicationname/WebForm1.aspx