通过Ajax传递参数

时间:2014-10-08 13:51:32

标签: javascript asp.net ajax

我正在研究一个项目并遇到问题。无法找出我的代码中有什么问题。下面是我的代码,不知道为什么Ajax url无法获取函数ReceivedMessageByIndexNumber的default.aspx。谢谢。

JavaScript的:

    ReceivedMessage(1);

    function ReceivedMessage(indexNumber) 
    {
        $.ajax({
            type: "Post",
            url: "Default.aspx/ReceivedMessageByIndexNumber?indexNumber="+indexNumber,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function (response) {
                var data = response.d;
                for (var i = 0; i < data.length; i++) {
                    alert(data[i]);
                }
            },
            failure: function (msg) {
                $('#output').text(msg);
            }
        });
    }

Default.aspx:

    [WebMethod]
    public static bool ReceivedMessageByIndexNumber(int textIndex)
    {
        string connectionString = @"Data Source=localhost;Initial Catalog=NotificationSystem;Integrated Security=True";
        using (SqlConnection connection = new SqlConnection(connectionString))
        using (SqlCommand command = connection.CreateCommand())
        {
            connection.Open();
            command.CommandText = @"SELECT TextWord FROM TextProperty WHERE TextIndex = '" + textIndex + "'";
            command.ExecuteNonQuery();
            return true;
        }
    }

3 个答案:

答案 0 :(得分:0)

这是一个HTTP POST方法,但您尝试传递参数,就像它是HTTP GET一样。我从来没有看到它写在你可以在URL中指定参数的地方,如HTTP GET。

注意我在下面添加了JSON数据行。这就是填写你的帖子请求的内容。

data: '{textIndex: "' + indexNumber + '" }',

整个功能:

function ReceivedMessage(indexNumber) 
{
    $.ajax({
        type: "Post",
        url: "Default.aspx/ReceivedMessageByIndexNumber",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: '{textIndex: "' + indexNumber + '" }',
        success: function (response) {
            var data = response.d;
            for (var i = 0; i < data.length; i++) {
                alert(data[i]);
            }
        },
        failure: function (msg) {
            $('#output').text(msg);
        }
    });
}

答案 1 :(得分:0)

您正在执行帖子请求。因此您需要在请求正文中添加参数而不是网址。所以改变你的ajax电话

function ReceivedMessage(indexNumber) 
    {
        $.ajax({
            type: "Post",
            url: "Default.aspx/ReceivedMessageByIndexNumber?indexNumber="+indexNumber,
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            data: "{'textIndex': " + indexNumber+ "}",
            success: function (response) {
                var data = response.d;
                for (var i = 0; i < data.length; i++) {
                    alert(data[i]);
                }
            },
            failure: function (msg) {
                $('#output').text(msg);
            }
        });
    }

答案 2 :(得分:0)

function ReceivedMessage(indexNumber) 
{
    $.ajax({
        type: "Post",
        url: "/Default/ReceivedMessageByIndexNumber/",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        data: { textIndex: indexNumber },
        success: function (response) {
            var data = response.d;
            for (var i = 0; i < data.length; i++) {
                alert(data[i]);
            }
        },
        failure: function (msg) {
            $('#output').text(msg);
        }
    });
}