gpt4 book ai didi

c# - 将参数传递给 ajax 上的 Web 服务

转载 作者:行者123 更新时间:2023-12-02 18:56:38 26 4
gpt4 key购买 nike

我有一个简单的 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 是动态的

我在这里做错了什么吗?

最佳答案

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

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

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

用它你不应该执行这个命令

从新闻中选择 *,其中 person = '"+ id +"' 按 NewsId desc 排序

考虑 id 是一个字符串

也试试这个

combonews = JSON.stringify(msg);

关于c# - 将参数传递给 ajax 上的 Web 服务,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15279592/

26 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com