无法使用jquery ajax调用aspx页面Web方法

时间:2022-10-19 22:33:02

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

这是Web方法代码

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

Can any one point out what may be wrong.

任何人都可以指出可能出错的地方。

3 个解决方案

#1


3  

Two things:

两件事情:

  1. You are missing the contentType in your jQuery .ajax() function.
  2. 您缺少jQuery .ajax()函数中的contentType。
  3. You need to account for the .d value in the JSON response.

    您需要在JSON响应中考虑.d值。

    $.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 .d syntax 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 .d property is there or not.

注意:.d语法是Microsoft在ASP.NET AJAX的ASP.NET 3.5版本中提供的反XSS保护;因此检查.d属性是否存在。

#2


2  

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

#3


0  

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

#1


3  

Two things:

两件事情:

  1. You are missing the contentType in your jQuery .ajax() function.
  2. 您缺少jQuery .ajax()函数中的contentType。
  3. You need to account for the .d value in the JSON response.

    您需要在JSON响应中考虑.d值。

    $.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 .d syntax 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 .d property is there or not.

注意:.d语法是Microsoft在ASP.NET AJAX的ASP.NET 3.5版本中提供的反XSS保护;因此检查.d属性是否存在。

#2


2  

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

#3


0  

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