0

当我通过 javascript/jquery 单击按钮时,我想触发此方法

这是我的网络方法,

 [WebMethod] 
public static string haldlescrolling(string name, string address)
{
    String str = string.Empty;

    httpWebRequest2 =             (HttpWebRequest)WebRequest.Create("http://ws.vidlib.com/video/list");
    httpWebRequest2.ContentType = "application/json";
    httpWebRequest2.Method = "POST";

    int start1 = start + 10;

    using (var streamWriter = new StreamWriter(httpWebRequest2.GetRequestStream()))
    {
        int max1 = max + 10;
        max = max + 10;
        string more = "{\"StartRowIndex\":\"" + start + "\",\"MaximumRows\":\"" + max + "\"}";
       // string json2 = js + more;
        streamWriter.Write(more);
        streamWriter.Flush();
        streamWriter.Close();
        var httpResponse = (HttpWebResponse)httpWebRequest2.GetResponse();
        using (var streamReader = new StreamReader(httpResponse.GetResponseStream()))
        {
            string result = streamReader.ReadToEnd();
            l1 = (List<Test>)Newtonsoft.Json.JsonConvert.DeserializeObject(result, typeof(List<Test>));
            for (int i = 0; i < l1.Count; i++)
            {
                str = str + "<img src='https://s3.amazonaws.com/movingmediapurchase/thumbnail/48748.jpg' />";
                string video_id = l1[i].ClipId.ToString();
                str = str + "<div id='parent'  style=\"position:relative; float:left; text-align: top;\" onmouseover='callMouseOver(\"" + video_id + "\")' onmouseout='callMouseOut(\"" + video_id + "\")' ><a href='" + l1[i].PreviewUrl.ToString() + "' class='html5lightbox' data-width='450' data-height='350'><img src='" + l1[i].ThumbnailUrl.ToString() + "' class ='thumbnail'/></a><div id='" + video_id + "' style='display: none;position: absolute; z-index:10000; top: 110px; left:30px ; height: 34px;'><img src='tweetbutton.png'/><img src ='small-facebook-like-butto.gif' /><img src='pinit-button.png' /></div></div>" + "&nbsp;";

                /*
                Label1.Text = l1.Count.ToString();
                Image1.ImageUrl = l1[0].ThumbnailUrl.ToString();
                Label1.Text = l1[0].ThumbnailUrl.ToString();
                 * 
                 */
            }
        }
    }
    return str;
}

如何创建javascript方法来调用上述方法背后的代码?

4

1 回答 1

1

如果您使用的是 jQuery(我会在您的情况下推荐),您可以使用这样的一些 javascript 代码:

$(function() {
    $('#idOfButton').click(function() {
        $.ajax({
            type: "POST",
            url: "Default.aspx/haldlescrolling",
            data: "{name: 'name', address: 'address'}",
            contentType: "application/json; charset=utf-8",
            dataType: "json",
            success: function(msg) {
                // do something on success
                $('#idOfDiv').html(msg.d);
            },
            error: function() {
                // do something on error
            }
        });
    });
});

第一行表示加载 DOM 时将执行一个函数:

$(function() {});

第二行将事件处理程序附加到具有 id 的按钮的单击事件idOfButton

$('#idOfButton').click(function() {});

$.ajax()调用是来自 jQuery(请参阅文档)的一种方法,用于使 ajax 调用更容易。

上面的代码不是纯 javascript,而是依赖于 jQuery。jQuery 是一个库,可以使您的 javascript 代码更容易和更具可读性。我绝对推荐你使用它,但还有其他库。

于 2013-04-23T10:42:52.323 回答