簡體   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