从 WebMethod 返回要在 jQuery 中使用的多个值

发布于 2024-10-20 02:01:03 字数 1041 浏览 9 评论 0原文

我有 jquery 使用 ajax/json 来获取元素 ID,然后点击:

[System.Web.Services.WebMethod]
public static string EditPage(string nodeID)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(Global.conString))
    using (SqlCommand cmd = new SqlCommand("contentPageGetDetail", con))
    {
        cmd.Parameters.Add("@ID", SqlDbType.UniqueIdentifier).Value = Global.SafeSqlLiteral(nodeID, 1);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            da.Fill(dt);
        }
    }
    if (dt.Count > 0)
    {
      string pageTitle = dt.Rows[0]["Title"].toString();
      string contentID = dt.Rows[0]["ContentID"].toString();
      return pageTitle, contentID, nodeID;
    }
    else
    {
      return "Failed";
    }
}

当返回时,我想将从存储过程返回的所有内容获取到成功部分中的 jquery 方法,并设置隐藏字段、下拉值和文本字段中的标题。

在 jQuery 中,我尝试使用“pageTitle”,但它未定义。在显示表单之前,我需要在 jQuery 方面执行哪些操作来获取返回的内容并填充 Web 表单中的字段?

I have jquery using ajax/json to grab an elements ID and then hits:

[System.Web.Services.WebMethod]
public static string EditPage(string nodeID)
{
    DataTable dt = new DataTable();
    using (SqlConnection con = new SqlConnection(Global.conString))
    using (SqlCommand cmd = new SqlCommand("contentPageGetDetail", con))
    {
        cmd.Parameters.Add("@ID", SqlDbType.UniqueIdentifier).Value = Global.SafeSqlLiteral(nodeID, 1);
        cmd.CommandType = CommandType.StoredProcedure;
        cmd.ExecuteNonQuery();
        using (SqlDataAdapter da = new SqlDataAdapter(cmd))
        {
            da.Fill(dt);
        }
    }
    if (dt.Count > 0)
    {
      string pageTitle = dt.Rows[0]["Title"].toString();
      string contentID = dt.Rows[0]["ContentID"].toString();
      return pageTitle, contentID, nodeID;
    }
    else
    {
      return "Failed";
    }
}

When it's time to return I want to grab all content returned from the stored procedure back to the jquery method in the success section and set a hiddenfield, dropdown value, and a title in a textfield.

In the jQuery I tried using "pageTitle" but it came up undefined. What do I need to do jQuery side to grab whats being returned and populate fields in my Web Form before showing the form?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(3

拥抱我好吗 2024-10-27 02:01:03

您需要创建一个结构或类来返回:

public struct TheStruct 
{ 
    public string pageTitle; 
    public int contentID, 
    public int nodeID; 
}

[System.Web.Services.WebMethod]
public static TheStruct EditPage(string nodeID)
{
    <your code here>

    var result = new TheStruct();
    result.pageTitle = "Test";
    result.contentID = 1;
    return result;
}

如果您

 contentType: "application/json; charset=utf-8",

在 AJAX 调用中传递:,您将获得一个 JSON 回复,您可以像这样解析它:

var obj = jQuery.parseJSON(webserviceReply);
alert(obj.pageTitle);

You'll need to create a struct or class to return:

public struct TheStruct 
{ 
    public string pageTitle; 
    public int contentID, 
    public int nodeID; 
}

[System.Web.Services.WebMethod]
public static TheStruct EditPage(string nodeID)
{
    <your code here>

    var result = new TheStruct();
    result.pageTitle = "Test";
    result.contentID = 1;
    return result;
}

If you pass:

 contentType: "application/json; charset=utf-8",

in the AJAX call, you'll get a JSON reply, which you can parse like:

var obj = jQuery.parseJSON(webserviceReply);
alert(obj.pageTitle);
最冷一天 2024-10-27 02:01:03
public class stuff {
string pagetitle;
string contentID;
string nodeID; 
}
[System.Web.Services.WebMethod]
public static stuff EditPage(string nodeID) {
... get the stuff
   stuff returnme = new stuff();
   returnme.pagetitle = ...
   returnme.contentid = ...
   return returnme;
}

==== jquery 端:
假设您正在使用 jquery 的 AJAX 调用,请执行以下操作:

.ajax({ type: "GET", url: "./return.asmx", async: true, dataType: "xml",
                success: function (respons, status) {
 $(respons).find("WebServiceCallName stuff pagetitle").text();
}});

您需要直接查看 Web 服务输出(只需导航到它,就好像它是网页一样)以确保您的 jQuery 选择器正确。

public class stuff {
string pagetitle;
string contentID;
string nodeID; 
}
[System.Web.Services.WebMethod]
public static stuff EditPage(string nodeID) {
... get the stuff
   stuff returnme = new stuff();
   returnme.pagetitle = ...
   returnme.contentid = ...
   return returnme;
}

==== jquery side:
Assuming you're using the AJAX call of jquery do something like this:

.ajax({ type: "GET", url: "./return.asmx", async: true, dataType: "xml",
                success: function (respons, status) {
 $(respons).find("WebServiceCallName stuff pagetitle").text();
}});

You'll need to look at the webservice output directly (just navigate to it as if it were a webpage) to make sure your jQuery selector is correct.

故事还在继续 2024-10-27 02:01:03

如果您想使用从 jquery 返回的字符串,请尝试:

success: function(msg) {
    alert(msg.d);
}

msg.d 将保存您从 Web 服务调用返回的字符串。如果要返回多个字符串,请尝试在它们之间添加预定义的字符串,并在 jquery success 函数中拆分它们。喜欢:

 yourfirststring||@@||yoursecondstring||@@||yourthirdstring

 var strings = msg.d.split("||@@||");

If you want to use the string you return from jquery try:

success: function(msg) {
    alert(msg.d);
}

msg.d will hold the string you return from your webservice call. If you want to return multiple strings, try to add a predefined string between them and split them in your jquery success function. like:

 yourfirststring||@@||yoursecondstring||@@||yourthirdstring

 var strings = msg.d.split("||@@||");
~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文