How to get return value from pagemethods in jQuery ..?

How to return true / false from PageMethods to the OnclientClick event .. ?? Here is a jQuery function that will raise the OnClientClick event for some button. If this function returns true, I want to execute some methods on the server side ..? but how to return values ​​to the OnClientClick event .. ??

Here is my code ..

function SendFriendReq(FriendReq) { PageMethods.CheckFriend(FriendReq, function (ResString) { // Here I Want to send True/False to OnClientEvent based on ResStirng value }); } 
+4
source share
2 answers

Using jQuery Ajax if your WebMethod is defined as follows:

 [WebMethod] public static string GetTime() { return DateTime.Now.ToString(); } 

You can call it from your javascript on the page as follows:

 $.ajax({ type: "POST", url: "MyPage.aspx/GetTime", contentType: "application/json; charset=utf-8", dataType: "json", success: function(data) { var time = data.d; } }); 

This is an alternative if you do not want to use ASP.NET auto-generated JavaScript proxies. I personally prefer using jQuery, as it is a universal solution that works with most Web Services .

+2
source

You can call your PageMethod with a return value, like this, if you do not want to pass any parameters, you can simply delete them:

 PageMethods.AddDocumentToCRM(imagedata, retrievedGuid, function(result) { if (result != null || result != undefined) appendMessage("<span style='color:#cE5E04'><b>Document Uploaded.</b></span> <br /><b>Note GUID : </b>" + result + "<br />"); else appendMessage("<span style='color:#cE5E04'<b>Document Uploaded Fail.</b></span>"); } ); 
-1
source

Source: https://habr.com/ru/post/1443787/


All Articles