javascript 无法使用 jquery ajax 调用 aspx 页面 web 方法

声明:本页面是StackOverFlow热门问题的中英对照翻译,遵循CC BY-SA 4.0协议,如果您需要使用它,必须同样遵循CC BY-SA许可,注明原文地址和作者信息,同时你必须将它归于原作者(不是我):StackOverFlow 原文地址: http://stackoverflow.com/questions/20658042/
Warning: these are provided under cc-by-sa 4.0 license. You are free to use/share it, But you must attribute it to the original authors (not me): StackOverFlow

提示:将鼠标放在中文语句上可以显示对应的英文。显示中英文
时间:2020-10-27 18:58:39  来源:igfitidea点击:

Unable to call aspx page web method using jquery ajax

c#javascriptjqueryasp.netajax

提问by iJade

Getting aspx page html when trying to call web method on aspx page.Here is the jQuery code

尝试在 aspx 页面上调用 web 方法时获取 aspx 页面 html。这是 jQuery 代码

  $(function () {
        $.ajax({
            type: 'POST',
            url: 'Default.aspx/Hello',
            data: "{}",
            async: false,
            success: function (response) {
                console.log('Success: ', response);
            },
            error: function (error) {
                console.log('Error: ', error);
            }
        });
    });

And here is the web method code

这是网络方法代码

 [System.Web.Services.WebMethod]
    public static string Hello()
    {
        string returnString = "hoiiiiiii";
        return returnString;
    }

Can any one point out what may be wrong.

任何人都可以指出什么可能是错误的。

回答by Karl Anderson

Two things:

两件事情:

  1. You are missing the contentTypein your jQuery .ajax()function.
  2. You need to account for the .dvalue in the JSON response.

    $.ajax({
        type: "POST",
        url: "Default.aspx/Hello",
        data: "{}",
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result.hasOwnProperty("d")) {
                // The .d is part of the result so reference it
                //  to get to the actual JSON data of interest
                console.log('Success: ', result.d);
            }
            else {
                // No .d; so just use result
                console.log('Success: ', result);
            }
        }
    });
    
  1. contentType的 jQuery.ajax()函数中缺少。
  2. 您需要考虑.dJSON 响应中的值。

    $.ajax({
        type: "POST",
        url: "Default.aspx/Hello",
        data: "{}",
        async: false,
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function(result) {
            if (result.hasOwnProperty("d")) {
                // The .d is part of the result so reference it
                //  to get to the actual JSON data of interest
                console.log('Success: ', result.d);
            }
            else {
                // No .d; so just use result
                console.log('Success: ', result);
            }
        }
    });
    


Note: The .dsyntax was an anti-XSS protection put in by Microsoft in the ASP.NET 3.5 release of ASP.NET AJAX; therefore the check to see if the .dproperty is there or not.

注意:该.d语法是微软在 ASP.NET AJAX 的 ASP.NET 3.5 版本中引入的一种反 XSS 保护;因此要检查该.d物业是否在那里。

回答by Venugopal M

You could code like this: (This works for me very well) I used jquery from CDN: "http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js"

你可以这样编码:(这对我很有效)我使用了 CDN 中的 jquery:“ http://ajax.googleapis.com/ajax/libs/jquery/1.4.1/jquery.min.js

public class MyClass
{
    public string myName
    {
        get { return "Hello"; }
    }
}

In your aspx.cs page:

在您的 aspx.cs 页面中:

[WebMethod]
[System.Web.Script.Services.ScriptMethod(ResponseFormat = System.Web.Script.Services.ResponseFormat.Json, UseHttpGet = true)]
public static object MyMethod()
{
    return new MyClass();
}

And in your ASPX page:

在您的 ASPX 页面中:

$.ajax({
            url: "somepage.aspx/MyMethod",
            data: {},
            cache: false,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            type: "GET",
            success: function (data) {
                if (data.hasOwnProperty("d"))
                    alert(data.d.myName);
                else
                    alert(data);
            },
            error: function (reponse) {
                alert(reponse);
            }
        });

回答by NiKhil Kutewalla

try this -

试试这个 -

$(function () {
    $.ajax({
        type: 'GET',
        url: 'Default.aspx/Hello',
        contentType: "application/json; charset=utf-8",
        async: false,
        dataType: "json",
        success: function (response) {
            console.log('Success: ', response);
        },
        error: function (error) {
            console.log('Error: ', error);
        }
    });
});

and the web method -

和网络方法 -

[WebMethod]
[ScriptMethod(UseHttpGet = true)]
public static string Hello()
{
    string returnString = "hoiiiiiii";
    return returnString;
}