0

我有一个带有一个参数的简单 Web 服务:

public static string LoadComboNews(string id)
{
    string strJSON = "";
    DataRowCollection people = Util.SelectData("Select * from News where person = "+ id +" Order by NewsId desc ");
    if (people != null && people.Count > 0)
    {
        //temp = new MyTable[people.Count];
        string[][] jagArray = new string[people.Count][];

        for (int i = 0; i < people.Count; i++)
        {
            jagArray[i] = new string[] { people[i]["NewsID"].ToString(), people[i]["Title"].ToString() };
        }

        JavaScriptSerializer js = new JavaScriptSerializer();
        strJSON = js.Serialize(jagArray);
    }

    return strJSON;
}

在javascript上我试图用参数调用它:

jQuery.ajax({
        type: "POST",
        url: "webcenter.aspx/LoadComboNews",
        data: "{'id':usrid}",
        contentType: "application/json; charset=utf-8",
        dataType: "json",
        success: function (msg) {
            // Replace the div's content with the page method's return.
            combonews = eval(msg.d);
        }

    });

更新:

usrid 是动态的

我在这里做错了吗?

4

2 回答 2

1

您发送的数据无效"{'id':usrid}"

这不是一个有效的 json 可能你想做的是假设 usrid 是一个变量

"{\"id\":"+usrid+"}"

用它你应该执行这个命令

Select * from News where person = '"+ id +"' Order by NewsId desc

考虑 id 是一个字符串

也试试这个

combonews = JSON.stringify(msg);
于 2013-03-07T19:08:27.430 回答
1

将 WebMethod 添加到方法中

[WebMethod]
public static string LoadComboNews(string id)

并尝试这种格式

$.ajax({
    type: "POST",
    url: "webcenter.aspx/LoadComboNews",
    data: '{"id":"usrid"}',
    contentType: "application/json; charset=utf-8",
    dataType: "json",
    success: function (msg) {
        // Replace the div's content with the page method's return.
        alert(msg.d);
    },
    error: function (jqXHR, textStatus, errorThrown) {
        console.log(textStatus, errorThrown);
        alert(textStatus + " " + errorThrown);
    }
});

或者

data: '{"id":"' + usrid + '"}',
于 2013-03-07T19:19:23.110 回答