开发者

Returning multiple values to be used in jQuery from a WebMethod

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?


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);


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.


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("||@@||");
0

上一篇:

下一篇:

精彩评论

暂无评论...
验证码 换一张
取 消

最新问答

问答排行榜