1

我有 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 表单中的字段?

4

3 回答 3

13

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

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);
于 2011-03-03T19:14:35.330 回答
2
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 选择器是正确的。

于 2011-03-03T19:20:53.157 回答
1

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

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

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

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

 var strings = msg.d.split("||@@||");
于 2011-03-03T19:16:44.907 回答