0

私は1つの引数を持つ単純な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 に答える