【发布时间】:2017-01-12 19:40:23
【问题描述】:
如何从使用 jQuery Get() 方法调用的 asp.net 页面返回一些值?下面给出了 get 方法,我希望 alert() 应该显示返回值。
get() 调用“result.aspx”页面,该页面返回一个字符串以及我想在 alert() 中显示的字符串。
$.get("result.aspx",
{
name: "Donald Duck",
city: "India"
},
function (result, status, xhr) {
alert(result);
}
);
result.aspx代码为:
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
LoadReturn();
}
}
void LoadReturn()
{
string name = Request.QueryString["name"];
string city = Request.QueryString["city"];
string returnValue="Hello Mr."+ name + " who lives in " + city;
//How to return value of returnValue to the jquery get() function.
}
如何将returnValue的值返回给jquery get()函数??
更新 我尝试了 response.write()。它能够将数据返回给 jquery get() 方法。
Response.Write(returnValue);
唯一的问题是它还向我发送了 result.aspx 页面的完整 HTML。这是我不想要的。 如何防止不必要的 HTML 到达 jquery get() 方法?
【问题讨论】:
-
使用
WebMethod或单独的 .svc webservice 文件。 aspx 页面旨在为整个页面加载提供整个 HTML 页面,而不是 HTML 或 JSON 的片段到 ajax 调用。本教程使用WebMethod提供了一个很好的简单示例。 aspsnippets.com/Articles/… -
我知道 .ajax() 方法,但是如何在 asp.net 中使用 .get() 返回?
-
你试过 Response.End() 跟随 Response.Write() 吗?如果您不只是在寻找快速解决方案,那么 ADysons 的答案可能是正确的方法。
-
@gb2d 我还没有尝试过 Response.End();。我现在这样做了,多余的不必要的页面 HTML 消失了。问题已解决。谢谢。