繁体   English   中英

在Jquery中使用webservice

[英]consuming webservice in Jquery

我使用了一个Jquery截断器解决方案,它由Henrik Nyh巧妙地编写,以便读取更多/更少的功能。 这里的剧本

我想修改此代码,以便能够通过更新数据库来跟踪读/未读状态。 我担心在webservice中进行数据库更新的代码如下所示。

[WebMethod]

public void updateReadStatus(int ID)
{
//code to update the database
}

我添加了以下内容来使用Jquery中的webservice

    function updateStatus(){
            $.ajax({
                type: "POST",
                url: "WebServices/myWebService/updateReadStatus",
                data: "{'ID': " + $("#lblID").Text + "}",
                contentType: "application/json; charset=utf-8",
                dataType: "json",
                success: OnSuccess,
                error: OnError
            });

function OnSuccess(reuslt) {
              alert(result.d);
            }

function OnError(result) {
              alert(result.status + ' ' + result.status);
            }
        }

//and i called this function on this line 

  full_node.find('a:last').click(function() {
  truncated_node.show(); updateStatus(); full_node.hide(); return false;

 //The user control that im using this scrip on has a repeater that contains a div that contains the paragraph to be truncated.

 <div class="readmore">
 <%# Eval("Message_Text")%>
 <asp:Label ID="lblID" runat="server" visible="false"
 </div>

截断脚本工作正常,它给了我我想要的功能,了解更多/更少。 但是我无法获得我想要的附加功能。 我收到“ 12030不明”错误,我相信问题出在行数据中:

"{'ID': " + $("#lblID").Text + "}", 

如何从标签的文本值中提取参数ID的值以将其传递给webservice?

text是一个函数,而不是一个属性,所以你需要用()调用它:

"{'ID': " + $("#lblID").text() + "}", 

此外,如果要通过Ajax调用给定的WebMethod,则需要使用[ScriptMethod]属性对其进行[ScriptMethod]

你没有说lblID识别出什么,但是你应该这样做:

$("#lblID").text()

要么

$("#lblID").val()

请参阅http://api.jquery.com/text/http://api.jquery.com/val/

暂无
暂无

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM