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

bal*_*der 1 c# asp.net ajax jquery webmethod

我有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";
    }
}
Run Code Online (Sandbox Code Playgroud)

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

在jQuery中我尝试使用"pageTitle",但它未定义.我需要做什么jQuery方面来获取返回的内容并在显示表单之前填充Web窗体中的字段?

And*_*mar 12

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

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;
}
Run Code Online (Sandbox Code Playgroud)

如果你通过:

 contentType: "application/json; charset=utf-8",
Run Code Online (Sandbox Code Playgroud)

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

var obj = jQuery.parseJSON(webserviceReply);
alert(obj.pageTitle);
Run Code Online (Sandbox Code Playgroud)